Created
January 24, 2017 17:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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