Hi,
First step I store Windows credentials in PRTG - so you can use them as parameters for your script. Using these credentials you can establish a remote Powershell session. Make sure your user account has the necessary rights for remote Powershell. For example local Admin. If you are working with Citrix cmdlets make sure your user has read only rights configured in Citrix Studio.
Sensor Parameters
-server '<FQDN>' -domain '%windowsdomain' -username '%windowsuser' -password '%windowspassword'
Powershell Script
Read and store the PRTG Sensor Parameters
param (
[string]$server,
[string]$domain,
[string]$username,
[string]$password
)
With these parameters from PRTG you can store them in a variable $credentials
$credentials = New-Object System.Management.Automation.PSCredential (($domain+'\'+$username), (ConvertTo-SecureString $password -AsPlainText -Force))
There are several ways to establishing a remote Powershell session. After some tests i decided to do it with Invoke-Command and run it as a job. It gives us some performance.
$job = Invoke-Command -Computername $server -credential $credentials -ScriptBlock { ... } -asjob
Inside the ScriptBlock you can get your Data. The ScriptBlock is running on the remote Server. For example if you want to use Citrix cmdlets you can just add them to your session on a Citrix Controller.
add-pssnapin citrix*
Now you can get all your Information like
Get-BrokerServiceStatus
Worker in maintenance= Get-BrokerMachine | Select -ExpandProperty "InMaintenanceMode"
Active Session = (Get-BrokerSession | where {$_.SessionState -eq "Active" }).Count
Some cmdlets like Get-BrokerServiceStatus will give Data in Tform of text. You can substitute these to numbers and translate them back later by a value lookupskript.
Now we have our information and need them to get back to PRTG. In my Opinion its quite comfortable to put them in an Object and return it.
$Data= [PSCustomObject]@{
Data_1 = $Data_1
...
}
return $Data
Now outside of the ScriptBlock we wait for the job to complete its work and give us our Data back
Wait-Job $job | Out-Null
$Data= Receive-Job -Job $job
Now you can create the XML Output for PRTG to display by accessing your Data like
$Data.Data_1
Best regards,
Phil
Add comment