Advertisement
Guest User

Untitled

a guest
Oct 5th, 2018
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
  2. [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
  3.  
  4. # Ignore SSL errors
  5. #[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
  6.  
  7. # Config
  8. $baseurl = "https://asdf.asdf.com"
  9. $username = "user"
  10. $password = "pass"
  11. $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
  12. $useragent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
  13.  
  14. $onlinecount=0
  15. $offlinecount=0
  16.  
  17. # Login to UniFi Controller
  18. $postparams = ConvertTo-Json -InputObject @{username=$username;password=$password}
  19. $result = Invoke-WebRequest -Uri "$($baseurl)/api/login" -ContentType "application/json" -Method POST -Body $postparams -SessionVariable session -UserAgent $useragent -UseBasicParsing
  20. if( (ConvertFrom-Json -InputObject $result.Content).meta.rc -ne "ok" ) {
  21.     Throw "Unable to login to UniFi Controller. $($result.RawContent)"
  22. }
  23.  
  24. # Get list of all sites
  25. $result = Invoke-WebRequest -Uri "$($baseurl)/api/self/sites" -Method GET -WebSession $session -UserAgent $useragent -UseBasicParsing
  26. $sites = (ConvertFrom-Json -InputObject $result.Content).Data
  27.  
  28. foreach ($site in $sites) {
  29.     # Query for status of all devices in the default site
  30.  
  31.     $result2 = Invoke-WebRequest -Uri "$($baseurl)/api/s/$($site.name)/stat/device" -Method POST -Body "" -WebSession $session -UserAgent $useragent -UseBasicParsing
  32.     $status = (ConvertFrom-Json -InputObject $result2.Content)
  33.     if( $status.meta.rc -ne "ok" ) {
  34.         Throw "Unable to retrieve UniFi device status. $($result2.RawContent)"
  35.     }
  36.  
  37.     Write-Host $site.desc
  38.  
  39.     # Loop through each device and output it's status
  40.     ForEach ( $ap in $status.data ) {
  41.         # Formatting for NAME
  42.         if( [string]::IsNullOrEmpty($ap.name) ) {
  43.             $apname = "(no name)"
  44.         } else {
  45.             $apname = $ap.name
  46.         }
  47.  
  48.         # Formatting for Model
  49.         $apmodel = "$($ap.type) $($ap.model)"
  50.         # Format the status line
  51.         $statusline = "$($apmodel.PadRight(10)) - $($ap.ip.PadRight(15)) ($($ap.mac.PadRight(17))) - v$($ap.version.PadRight(15)) - $($apname)"
  52.  
  53.         If( $ap.uptime -gt 0 ) {
  54.             # Device is online
  55.             Write-Host "ONLINE: $statusline"
  56.         } Else {
  57.             # Device is offline
  58.             Write-Warning "OFFLINE: $statusline"
  59.         }
  60.     }
  61.     Write-Host
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement