Hi Dariusz
I was able to make this script work making some little changes,
this is the new code used
# Parameter "-computername" for the remote hosts address and "-proc" for the dynamic process name
param(
$computername = "localhost",
$proc = "proc"
)
# Get processes of the remote machine that are like the provided dynamic process name
$Processes = (Get-WmiObject -ComputerName $computername -Query "SELECT * FROM Win32_Process WHERE NAME LIKE '%$($proc)%'").count
# Check if the process is found or not, report it back to PRTG.
if([string]::IsNullOrEmpty($Processes)) {
Write-Host ([string]::Format("{0}:{0} Process not found {1}.",0,$proc));
} else {
Write-Host ([string]::Format("{0}:{0} Process found {1}.",$Processes,$proc))
};
Here described the changes made:
- It was missing the last parenthesis } and this prevented the script from running
- Added .count at the end of the creation of variable $Processes so to have it counted
- Changed from ("{0}:{0} Process found {1}.",1,$proc) to ("{0}:{0} Process found {1}.",$Processes,$proc) the last Format to have the result of processes count included in the output
So far the script works perfectly when used in a sensor on a local probe but it gives back no results nor error when used on a remote machine so I guessed it's an authentication problem.
Checking the logs in fact I see Access denied
Get-WmiObject : Accesso negato. (Eccezione da HRESULT: 0x80070005
(E_ACCESSDENIED))
In C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\Dynamic
Windows Process.ps1:20 car:15
+ $Processes = (Get-WmiObject -ComputerName $computername -Query "SELECT *
FROM Wi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedA
ccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Pow
erShell.Commands.GetWmiObjectCommand
I've tried different other ways to get it
- Changed setting in sensor from "Use security context of probe service" to "Use Windows credentials of parent device" and got PE095 error
- Added credentials variables in the script
but the only way that made it working is running the probe service as a local administrator
I reached this conclusion cause testing it from powershell cli it was working smoothly but not from PRTG interface
Let me know if you think that something is not correct or can be done more properly
Regards
Add comment