Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell Script to get report on Ubiquiti Unifi clients. Associated blogpost https://blog.darrenjrobinson.com/accessing-your-ubiquiti-unifi-network-configuration-with-powershell/
# Clients to Monitor
$uInterestingClients = @()
$uInterestingClients += "myPhone"
$uInterestingClients += "otherPhone"
$uInterestingClients += "anotherPhone"
# Unifi Controller Login Base URI
$uController = 'yourControllerIP' # e.g 'https://192.168.1.2:8443'
# Identifier of the site in UniFi. Set to Default to use the default site
$uSiteID = "default"
$uUsername = 'adminuser' # yourAdmin UserID
$uPassword = 'yourPassword' # yourAdmin User Password
$uAuthBody = @{"username" = $uUsername; "password" = $uPassword }
$uHeaders = @{"Content-Type" = "application/json" }
# Allow connection with the Unifi Self Signed Cert
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12
# Current DateTime for reference to Client State based on Last Seen Time
$currentDateTime = Get-date
# Create function to receive UNIX Time Format and return it for the local timezone
function Convert-UnixTime {
Param(
[Parameter(Mandatory = $true)][int32]$unixDate
)
$orig = (Get-Date -Year 1970 -Month 1 -Day 1 -hour 0 -Minute 0 -Second 0 -Millisecond 0)
$timeZone = Get-TimeZone
$utcTime = $orig.AddSeconds($unixDate)
$localTime = $utcTime.AddHours($timeZone.BaseUtcOffset.Hours)
# Return local time
return $localTime
}
$uLogin = Invoke-RestMethod -Method Post -Uri "$($uController)/api/login" -Body ($uAuthBody | convertto-json) -Headers $uHeaders -SessionVariable UBNT
if ($uLogin.meta.rc.Equals("ok")) {
Write-Host -ForegroundColor Green "Successfully authenticated to $($uController) as $($uUsername)"
$uStatusObject = [pscustomobject][ordered]@{
hostname = $null
status = $null
lastHome = $null
homeSince = $null
}
# Get Registered Clients
$uClients = Invoke-RestMethod -Method Get -Uri "$($uController)/api/s/$($uSiteID)/stat/alluser" -WebSession $UBNT -Headers $uHeaders
# Get Active Clients
$uActiveClients = Invoke-RestMethod -Method Get -Uri "$($uController)/api/s/$($uSiteID)/stat/sta" -WebSession $UBNT -Headers $uHeaders
$uClientStatus = @()
foreach ($uclient in $uInterestingClients) {
$lastSeen = $null
$uAClientState = $null
$uRClientState = $null
$uUser = $uStatusObject.PsObject.Copy()
$uUser.hostname = $uclient
# Active Client Data
$uAClientState = $uActiveClients.data | Select-Object | Where-Object { $_.hostname -eq $uclient }
# Registered Client Data
$uRClientState = $uClients.data | Select-Object | Where-Object { $_.hostname -eq $uclient }
# if interesting client not found in active clients search all clients
if ($uAClientState) {
$lastSeen = Convert-UnixTime($uAClientState.last_seen)
if ($lastSeen -ge $currentDateTime.AddMinutes(-5)) {
$uUser.status = "Home"
}
else {
$uUser.status = "Away"
}
# if ($uclient -eq "AbbeysiPhone6s") {break }
$uUser.lastHome = $lastSeen
[string]$uUser.homeSince = Convert-UnixTime($uAClientState.assoc_time).ToString()
}
else {
$uUser.status = "Away"
$lastSeen = Convert-UnixTime($uRClientState.last_seen)
[string]$uUser.lastHome = $lastSeen.ToString()
}
$uClientStatus += $uUser
}
write-host -ForegroundColor Cyan $uClientStatus
$uClientStatus
}
else {
Write-Host -ForegroundColor Red "Unsuccessfull in authenticating to $($uController) as $($uUsername)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.