Okay - now - if you execute the script in a PowerShell - you see the correct WRITE-HOST output. But the EXIT CODE is not directly visible. To read the EXIT CODE you use a special command right after you executed the script:
$LASTEXITCODE
This will show you the last exit code.
Now - I tested your script and it works just fine... telling me that your issue is not the script itself.
I still think you could work WITHOUT the two EXIT statements after all. But well that's a different story.
Did you ever enable logging in PRTG and see what the logfiles tell you for this sensor? I am not convinced that PRTG is interpreting the script wrong.
Cause I don't want to let you down - look at the following script - note that this is an ADVANCED EXE script and needs to be placed in the according directory.
param(
$Computer,
$ProcessName,
$ProcessCommandLineFilter1, #optional
$ProcessCommandLineFilter2 #optional - but fill 1 first
)
#function to convert a string to proper XML and write it as output/screen
Function WriteXmlToScreen ([xml]$xml) #just to make it clean XML code...
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
If ($ProcessCommandLineFilter1.Length > 0) {
If ($ProcessCommandLineFilter2.Length > 0) {
$Processes = (WmiObject -ComputerName $Computer Win32_Process -Filter "name = '$ProcessName'" | Select-Object CommandLine | findstr /i "$ProcessCommandLineFilter1" | findstr /i "$ProcessCommandLineFilter2");
} Else {
$Processes = (WmiObject -ComputerName $Computer Win32_Process -Filter "name = '$ProcessName'" | Select-Object CommandLine | findstr /i "$ProcessCommandLineFilter1");
}
} Else {
$Processes = (WmiObject -ComputerName $Computer Win32_Process -Filter "name = '$ProcessName'");
}
If([string]::IsNullOrEmpty($Processes)) {
$XML = "<prtg>
<result>
<channel>ProcessId</channel>
<value>-1</value>
<LimitMode>1</LimitMode>
<LimitMinError>1</LimitMinError>
<LimitErrorMsg>Process not found</LimitErrorMsg>
</result>
<result>
<channel>Handles</channel>
<value>0</value>
</result>
<text>Process not found</text>
</prtg>";
} Else {
$XML = "<prtg>
<result>
<channel>ProcessId</channel>
<value>" + $Processes.ProcessId + "</value>
<LimitMode>1</LimitMode>
<LimitMinError>1</LimitMinError>
<LimitErrorMsg>Process not found</LimitErrorMsg>
</result>
<result>
<channel>Handles</channel>
<value>" + $Processes.Handles + "</value>
</result>
<text>command line: " + $Processes.CommandLine + " - creation date: " + $Processes.CreationDate + "</text>
</prtg>";
}
WriteXmlToScreen $XML;
You could modify the result/channel configuration and report back anything numeric... I just did go with HANDLES as an example..
The script will report back a PROCESS ID (PID) if your process was found - if not it will report back -1 and automatically raise an error. The sensor status text will go to process not found.
If the process is found, the PID and HANDLES are available, the sensor text will hold the whole command line information and the CreationDate what is the start date / time of the process. In theory you could even monitor that, but that's another story..
This script will expect that you set parameters... in your case:
mark-promark02 prowin32.exe "job.pf" "probi"
In theory this could work as parameter as well:
%host prowin32.exe "job.pf" "probi"
Hope this helps you...
Regards
Florian
Add comment