The script works fine directly on the server, but not when run through SAM.
My debugging has highlighted the $Process command is not being set, the only thing i can think of is the | [BAR] character is not being passed correctly?
Any Thoughts and suggestions are welcome.
Thanks
David
Function UpTime($ProcessTitle)
{
$Process = Get-Process | Where-Object {$_.MainWindowTitle -LIKE "*$ProcessTitle*" } -ErrorAction SilentlyContinue
$Date = Get-Date
Set-Content -Path "C:\MyTextFile.txt" -Value "DEBUG-Process:$Process " -Force
If($Process -eq $null)
{
$Uptime = '0'
}
Else
{
If($Process.Count -eq $null)
{
$Uptime = ($Date.Subtract($Process.StartTime)).TotalMinutes
}
Else
{
$Sorted = $Process | Sort-Object StartTime
$Process = $Sorted[0]
$Uptime = ($Date.Subtract($Process.StartTime)).TotalMinutes
}
$Uptime = [int]$Uptime
}
Return $Uptime
}
$ProcessTitle = $args[0]
$Uptime = UpTime $ProcessTitle
Write-Host 'Message: The longest instance of process' $ProcessTitle 'has been running for (in minutes)'
Write-Host 'Statistic: '$Uptime