четверг, 15 февраля 2018 г.

PRTG sensor secrets/examples
example for grace period windows terminal server

poweshell script sensor for prtg:
Get-Content -LiteralPath "C:\scripts\scriptout.txt"

windows sheduler contains everyday task:
powershell -windowstyle minimized -c "powershell -c c:\scripts\grace-period-ts.ps1 > c:\scripts\scriptout.txt"

powershell script for non-domain host:
prepare password:
$pass = "MYP@SSW0RD" | ConvertTo-SecureString -AsPlainText -Force
ConvertFrom-SecureString -SecureString $pass

* Attention, can be reverse unsecured:
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

copy output for variable $pass for script below
SCRIPT:
$hostname = "ts01"
$prtg = '<?xml version="1.0" encoding="Windows-1252" ?> 
<prtg>'

$username="$hostname\Administrator"
$pass="01000ac0d08c9ddf0115d1118c7a00c04fc297eb01000000af5d5e1acfacf44fb3e6da2b3bb6c12d0000000002000000000003660000c000000010000000ce6f9d71d1da7703458f899c695f4a220000000004800000a000000010000000f26cf27ef25096d4b56c2634b9167fb61800000038f442cef9a1a121ef4238bd21f84b6bf0958f7de57e201914000000aeb56a8714f507a453186e4151614953734b5a35"
$cryptpass=$pass | ConvertTo-SecureString
$cred=New-Object System.Management.Automation.PSCredential -ArgumentList $username, $cryptpass


$gp = [int](Invoke-WmiMethod -Credential $cred -PATH (gwmi -ComputerName $hostname -Credential $cred -namespace root\cimv2\terminalservices -class win32_terminalservicesetting).__PATH -name GetGracePeriodDays).daysleft

$prtg+="
<result> 
   <channel>TS Grace Period Channel</channel>
   <unit>Custom</unit>
   <customUnit>Days</customUnit> 
   <float>0</float>
   <value>"+$gp+"</value> 
   <showChart>1</showChart> 
   <showTable>1</showTable> 
   <Limitmode>1</Limitmode>
   <LimitMinError>14</LimitMinError>
   <LimitErrorMsg>ATTENTION! Grace period less or equal 14 days!($gp)</LimitErrorMsg>
</result>
</prtg>"

$prtg

exit 0


For domain autentication we may use native wmi or winrm
example for winrm:
$s = New-PSSession -Computername $hostname
$gp = Invoke-Command -Session $s -ScriptBlock{[int](Invoke-WmiMethod -PATH (gwmi -namespace root\cimv2\terminalservices -class win32_terminalservicesetting).__PATH -name GetGracePeriodDays).daysleft}
Remove-PSSession $s
etc...