Had a request from someone around how to obtain a history of public IPs for devices at scale resulting in the following script for NinjaOne custom fields.
```powershell
# Get the last public IP address.
$lastIP = Ninja-Property-Get lastIP
# Get the current public IP address.
try {
$currentIP = (Resolve-DnsName -Name myip.opendns.com -Server 208.67.222.220 -ErrorAction Stop).IPAddress
}
catch {
Write-Warning "Failed to get current public IP. Check your internet connection."
Write-Warning "$_"
Exit 1
}
# Compare the IPs.
if ($currentIP -eq $lastIP) {
Write-Output "Public IP address has not changed: $currentIP"
Exit 0
}
Write-Output "Public IP address has changed from '$lastIP' to '$currentIP'"
# Set the current public IP address to the new value.
Ninja-Property-Set lastIP $currentIP
# Get list of IPs and append the last IP.
# Grab the last 29
$multilineString = Ninja-Property-Get listofips
if ($multilineString) {
[System.Collections.ArrayList]$lines = @($multilineString -split "`n" | Where-Object { -not ([String]::IsNullOrWhiteSpace($_)) } | Select-Object -Last 29)
}
else {
$lines = [System.Collections.ArrayList]::new()
}
$lines.Add((Get-Date -UFormat "%Y-%m-%d %H:%M:%S") + " " + $currentIP) | Out-Null
$multilineString = $lines -join "`n"
Ninja-Property-Set listofips $multilineString
Exit 0
```
---
[[Epistemic status|Colophon: Brewed]]