Last active
February 5, 2024 15:26
-
-
Save KelvinTegelaar/ec04c8c3fd3ea87c6d8e15f6a1001fc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############### | |
$UnifiBaseUri = "https://Controller.com:8443/api" | |
$UnifiUser = "APIUSER" | |
$UnifiPassword = "APIPASSWORD" | |
############## | |
$UniFiCredentials = @{ | |
username = $UnifiUser | |
password = $UnifiPassword | |
remember = $true | |
} | ConvertTo-Json | |
write-host "Logging in to Unifi API." -ForegroundColor Green | |
try { | |
Invoke-RestMethod -Uri "$UnifiBaseUri/login" -Method POST -Body $uniFiCredentials -SessionVariable websession | |
} | |
catch { | |
write-host "Failed to log in on the Unifi API. Error was: $($_.Exception.Message)" -ForegroundColor Red | |
} | |
write-host "Collecting sites from Unifi API." -ForegroundColor Green | |
try { | |
$sites = (Invoke-RestMethod -Uri "$UnifiBaseUri/self/sites" -WebSession $websession).data | |
} | |
catch { | |
write-host "Failed to collect the sites. Error was: $($_.Exception.Message)" -ForegroundColor Red | |
} | |
$UnifiData = foreach ($site in $sites) { | |
write-host "processing $($site.desc)" | |
$unifiDevices = Invoke-RestMethod -Uri "$UnifiBaseUri/s/$($site.name)/stat/device" -WebSession $websession | |
$UnifiSwitches = $unifiDevices.data | Where-Object { $_.type -eq "usw" } | |
$uaps = $unifiDevices.data | Where-Object { $_.type -eq "uap" } | |
[PSCustomObject]@{ | |
SiteName = $site.desc | |
'Number of Switches' = [int]$Unifiswitches.serial.count | |
'Number of APs' = [int]$uaps.serial.count | |
'Total devices' = [int]$unifiDevices.data.serial.count | |
'Name of switches' = $UnifiSwitches.name -join ' ' | |
'Name of APs' = $uaps.name -join ' ' | |
'MACs of Switches' = $UnifiSwitches.mac -join ' ' | |
'MACs of APs' = $uaps.mac -join ' ' | |
} | |
} | |
$UnifiData | export-csv "C:\Temp\UnifiData.csv" -Delimiter ',' -NoTypeInformation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment