Skip to content

Instantly share code, notes, and snippets.

@KelvinTegelaar
Last active February 5, 2024 15:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KelvinTegelaar/ec04c8c3fd3ea87c6d8e15f6a1001fc3 to your computer and use it in GitHub Desktop.
Save KelvinTegelaar/ec04c8c3fd3ea87c6d8e15f6a1001fc3 to your computer and use it in GitHub Desktop.
###############
$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