Skip to content

Instantly share code, notes, and snippets.

@igoravl
Created January 24, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igoravl/be273447675aad9c797f744a61a6cc5f to your computer and use it in GitHub Desktop.
Save igoravl/be273447675aad9c797f744a61a6cc5f to your computer and use it in GitHub Desktop.
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