Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
- [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
- # Ignore SSL errors
- #[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
- # Config
- $baseurl = "https://asdf.asdf.com"
- $username = "user"
- $password = "pass"
- $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
- $useragent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
- $onlinecount=0
- $offlinecount=0
- # Login to UniFi Controller
- $postparams = ConvertTo-Json -InputObject @{username=$username;password=$password}
- $result = Invoke-WebRequest -Uri "$($baseurl)/api/login" -ContentType "application/json" -Method POST -Body $postparams -SessionVariable session -UserAgent $useragent -UseBasicParsing
- if( (ConvertFrom-Json -InputObject $result.Content).meta.rc -ne "ok" ) {
- Throw "Unable to login to UniFi Controller. $($result.RawContent)"
- }
- # Get list of all sites
- $result = Invoke-WebRequest -Uri "$($baseurl)/api/self/sites" -Method GET -WebSession $session -UserAgent $useragent -UseBasicParsing
- $sites = (ConvertFrom-Json -InputObject $result.Content).Data
- foreach ($site in $sites) {
- # Query for status of all devices in the default site
- $result2 = Invoke-WebRequest -Uri "$($baseurl)/api/s/$($site.name)/stat/device" -Method POST -Body "" -WebSession $session -UserAgent $useragent -UseBasicParsing
- $status = (ConvertFrom-Json -InputObject $result2.Content)
- if( $status.meta.rc -ne "ok" ) {
- Throw "Unable to retrieve UniFi device status. $($result2.RawContent)"
- }
- Write-Host $site.desc
- # Loop through each device and output it's status
- ForEach ( $ap in $status.data ) {
- # Formatting for NAME
- if( [string]::IsNullOrEmpty($ap.name) ) {
- $apname = "(no name)"
- } else {
- $apname = $ap.name
- }
- # Formatting for Model
- $apmodel = "$($ap.type) $($ap.model)"
- # Format the status line
- $statusline = "$($apmodel.PadRight(10)) - $($ap.ip.PadRight(15)) ($($ap.mac.PadRight(17))) - v$($ap.version.PadRight(15)) - $($apname)"
- If( $ap.uptime -gt 0 ) {
- # Device is online
- Write-Host "ONLINE: $statusline"
- } Else {
- # Device is offline
- Write-Warning "OFFLINE: $statusline"
- }
- }
- Write-Host
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement