Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checks whether the IP address of the given server has changed since the last time this script was called
<#
.SYNOPSIS
Checks whether the IP address of the given server has changed since the last time this script was called
#>
Function Resolve-ServerAddress
{
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string]
$Server,
[Parameter()]
[string]
$RegistryKey = 'HKCU:\Software\Scripts\Resolve-ServerAddress'
)
Begin
{
$regKeyPath = "$RegistryKey\$Server"
If (-not (Test-Path $regKeyPath))
{
$key = New-Item $regKeyPath -Force | Out-Null
}
}
Process
{
try
{
$ipAddress = [System.Net.Dns]::GetHostAddresses($Server).IPAddressToString
}
catch
{
throw "Error retrieving IP address for $Server`: $Error"
}
$lastIpAddress = (Get-ItemProperty $regKeyPath).IPAddress
$lastCheckedAt = (Get-ItemProperty $regKeyPath).LastCheckedAt
New-ItemProperty $regKeyPath -Name 'LastRun' -Value "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))" -Force | Out-Null
New-ItemProperty $regKeyPath -Name 'IPAddress' -Value $ipAddress -Force | Out-Null
if ($ipAddress -ne $lastIpAddress)
{
New-ItemProperty $regKeyPath -Name 'LastIPAddress' -Value $lastIpAddress -Force | Out-Null
}
return [PSCustomObject] @{
Server = $Server;
LastRun = $lastCheckedAt;
LastIpAddress = $lastIpAddress;
IpAddress = $ipAddress;
ChangedSinceLastCheck = ($ipAddress -ne $lastIpAddress)
}
}
}
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.