param(
[string]$server = '10.10.1.100',
[string]$port = '8443',
[string]$site = 'default',
[string]$username = 'admin',
[string]$password = 'c3rv3c3r14',
[switch]$debug = $false
)
- Ignore SSL Errors
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
- Define supported Protocols
[System.Net.ServicePointManager]::SecurityProtocol = @("Tls12","Tls11","Tls","Ssl3")
- Confirm Powershell Version.
if ($PSVersionTable.PSVersion.Major -lt 3) {
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>Powershell Version is $($PSVersionTable.PSVersion.Major) Requires at least 3. </text>"
Write-Output "</prtg>"
Exit
}
- Create $controller and $credential using multiple variables/parameters.
[string]$controller = "https://$($server):$($port)/manage/account/login"
[string]$credential = "`{`"username`":`"$username`",`"password`":`"$password`"`}"
- Start debug timer
$queryMeasurement = [System.Diagnostics.Stopwatch]::StartNew()
- Perform the authentication and store the token to myWebSession
try {
$null = Invoke-Restmethod -Uri "$controller" -method post -body $credential -ContentType "application/json; charset=utf-8" -SessionVariable myWebSession
}catch{
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>Authentication Failed: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
- Query API providing token from first query.
try {
$jsonresultat = Invoke-Restmethod -Uri "https://10.10.1.100:8443/manage/site/default/devices/1/50" -WebSession $myWebSession
}catch{
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>API Query Failed: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
- Load File from Debug Log
- $jsonresultatFile = Get-Content '.\unifi_sensor2017-15-02-05-42-24_log.json'
- $jsonresultat = $jsonresultatFile | ConvertFrom-Json
- Stop debug timer
$queryMeasurement.Stop()
- Iterate jsonresultat and count the number of AP's.
- $_.state -eq "1" = Connected
- $_.type -like "uap" = Access Point ?
$apCount = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.state -eq "1" -and $_.type -like "uap"})){
$apCount ++
}
$apUpgradeable = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.state -eq "1" -and $_.type -like "uap" -and $_.upgradable -eq "true"})){
$apUpgradeable ++
}
$userCount = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.type -like "uap"})){
$userCount += $entry.'num_sta'
}
$guestCount = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.type -like "uap"})){
$guestCount += $entry.'guest-num_sta'
}
- Write Results
write-host "<prtg>"
Write-Host "<result>"
Write-Host "<channel>Access Points Connected</channel>"
Write-Host "<value>$($apCount)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Access Points Upgradeable</channel>"
Write-Host "<value>$($apUpgradeable)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Clients (Total)</channel>"
Write-Host "<value>$($userCount)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Guests</channel>"
Write-Host "<value>$($guestCount)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Response Time</channel>"
Write-Host "<value>$($queryMeasurement.ElapsedMilliseconds)</value>"
Write-Host "<CustomUnit>msecs</CustomUnit>"
Write-Host "</result>"
write-host "</prtg>"
- Write JSON file to disk when -debug is set. For troubleshooting only.
if ($debug){
[string]$logPath = ((Get-ItemProperty -Path "hklm:SOFTWARE\Wow6432Node\Paessler\PRTG Network Monitor\Server\Core" -Name "Datapath").DataPath) + "Logs (Sensors)\"
$timeStamp = (Get-Date -format yyyy-dd-MM-hh-mm-ss)
$json = $jsonresultat | ConvertTo-Json
$json | Out-File $logPath"unifi_sensor$($timeStamp)_log.json"
}
Add comment