Hi there, I'm trying to find if a user is still using his VM by pulling his last logon and I'm having trouble with the output. When I run it in PS it works as expected, but in Prtg it only shows the message text without the date of last loogin. Here's the code :

Import-Module ActiveDirectory

$value=1 $server=$env:COMPUTERNAME $LogonDate=Get-ADComputer $server -Properties LastLogonDate | select -ExpandProperty LastLogonDate $LogonDateString=$LogonDate.ToString() $DaysPassed=(New-TimeSpan -Start $LogonDate -End (Get-Date)).Days if ($DaysPassed -gt 60 ) {$value=0 $message="Vm was not used for more then 60 days, last Logon was on " + $LogonDateString} else { if ($DaysPassed -le 60 ){ $value=1 $message="Vm is in use, last Logon was on " + $LogonDateString}}

Write-Host @" <prtg> <result> <channel>Vm in use</channel> <value>$value</value> </result>

<text>$message</text> </prtg> "@


Article Comments

Dear claude,

for debugging, please go to the sensor settings and enable "Write Exe result to disk". After the next scan, find the logs in "C:\ProgramData\Paessler\PRTG Network Monitor\Logs\sensors".

You will probably see a Powershell error message. Please check if it helps to change the security context in the sensor settings (probe context or Windows credentials).


May, 2020 - Permalink

I already checked the log and also changed security context with no luck.

If I run the script in Powershell, I get the desired result :

<text>Vm is in use, last Logon was on 5/19/2020 2:21:21 AM</text> </prtg>

but in PRTG I get:

"Vm is in use, last Logon was on" without the date .


May, 2020 - Permalink

Dear claude,

in this case, expand the script and implement debug output. Please don't write the debug log into the script folder, as that one is protected, rather use C:\Temp for output.

For example, please write the full output of Get-ADComputer to that file, so you can identify where the script execution is going wrong.

For analysis, please also run the script manually from the 32-bit Powershell to see if you get the result, because PRTG calls the 32-bit Powershell.


May, 2020 - Permalink