This script checks what the current public IP address is. The plan is for this to run every 5 minutes or so, and then change some host file configuration depending on whether we the IP is identified as that of officeone
or officetwo
.
Each attempt at retrieving the IP address (as well as each success/failure) is logged to a daily log file which is overwritten every week.
To make it easier to manage, I split the code out into functions:
SendIpQuery
- for actually sending a webrequest to the IP serviceGet-PublicIP
- callsSendIpQuery
, which will call the above function for each of the services in$website
until it finds something or times out (15 second timeout) IP is identified as per the defined regular expressionSetServerConfiguration
- this will call the final method depending on whether the IP found relates toofficeone
orofficetwo
ConfigureServers
- configures the server host files based on which office the IP represents (the order of the parameters passed shows which office's IP was available)
The program begins right at the bottom of the script, and will run 5 times before giving up (if no IP is found).
I was hoping for some feedback on the structure of this script as well as methods used for timeout/requests and how appropriate they are.
# For logging each server attempt
$logname = (Get-Date).DayOfWeek
Function Log-Line {
param([string]$line)
Add-Content -Path "C:\servertesting\logs\$logname.txt" -Value $line -Force
}
# Region Functions
# Invokes a webrequest for the given website/service and returns the results.
# Timeout is set to 30 seconds - if this is reached the function returns 'timeout'
Function SendIPQuery {
param([string]$webaddress)
Log-Line "Requesting data from $webaddress..."
Try {return Invoke-WebRequest $webaddress -TimeoutSec 15}
Catch {return 'timeout'}
}
# Calls SendIPQuery for each of the websites in the array, and checks it against a regular expression
# Possible outcomes are valid IP (return), 'timeout' (continue) and invalid/missing IP (continue)
Function Get-PublicIP {
$ipregex = "[0-9]{2}[.][0-9]{3}[.][0-9]{2}[.][0-9]{2}"
$websites = @('http://ifconfig.me/ip',
'http://checkip.dyndns.com',
'http://icanhazip.com')
ForEach ($site in $websites) {
$webresponse = SendIPQuery $site
If ($webresponse.Content -match $ipregex)
{Log-Line "Valid IP found $($Matches[0])"; return $Matches[0]}
ElseIf ($webresponse -match 'timeout')
{Log-Line "Response timed out after 15 seconds..."; continue}
Else
{Log-Line "Invalid or Missing IP"; continue}
}
return
}
# Compares the give IP to each of the officeone and officetwo IPs and calls ConfigureServers appropriately
Function Set-ServerConfiguration {
param([string]$currentip)
$officeoneip = "11.111.11.11"
$officetwoip = "22.222.22.22"
If ($currentip -match $officeoneip) {
Log-Line "Configuring servers for officeone"
ConfigureServers 'officeone' 'officetwo'
}
ElseIf ($currentip -match $officetwoip) {
Log-Line "Configuring servers for officetwo"
ConfigureServers 'officetwo' 'officeone'
}
Else {
Log-Line "IP retrieved does not match our records"
}
}
# Copying over host file and removing Office.txt file depending on IP results
Function ConfigureServers {
param([string]$office, [string]$notoffice)
If (Test-Path "C:\servertesting\$notoffice.txt") {
Copy-Item "C:\servertesting\testy\hosts - $office" "C:\servertesting" -Force
Remove-Item "C:\servertesting\$notoffice.txt" -Force
New-Item "C:\servertesting\$office.txt" -ItemType File -Force
}
}
#endregion
Log-Line "----------------------------------------------------------------------------START $(Get-Date -Format ddMMyyyy-HHmmss)"
currentip = $null
$counter = 0
While (($currentip -eq $null) -and ($counter -lt 5)) {
# Get the IP
$currentip = Get-PublicIP
# Configure the required files
Set-ServerConfiguration $currentip
# To limit the number of attempts
$counter++
}
Log-Line "------------------------------------------------------------------------------END $(Get-Date -Format ddMMyyyy-HHmmss)"