Wednesday, April 4, 2012

Brain dump to help others should google pick it up:

I was looking for ways to script updating a VMWare View 5 security server with a dynamic IP address. Searching I found this posting which provides the following script: http://www.vtexan.com/2011/12/07/using-vmware-view-5-security-server-with-dynamic-ips/

1: #Add-PSSnapin VMware.VimAutomation.Core
2: #Add-PSSnapin VMware.View.Broker
3:
4: # Name of the Security Server
5: # $SecurityServer = "SecurityServerNameHere"
6:
7: # For logging creating a timestamp
8: $TimeStamp = Get-Date -format yyyy-MM-dd-H-mm
9:
10: # Filling $CheckedIP with the external IP address, using whatismyip.com service
11: $wc = New-Object net.WebClient
12: $CheckedIP = $wc.downloadstring("http://automation.whatismyip.com/n09230945.asp")
13:
14: # Now check the current ExternalPCoIPURL entry
15: $CurrentSettings = Get-ConnectionBroker
16: $CurrentIP = $CurrentSettings[0].externalPCoIPURL
17: # Check if $CurrentIP starts with the IP address from $CheckedIP
18: # Used StartsWith because $CurrentIP has port address at the end
19: $Result = $CurrentIP.StartsWith($CheckedIP)
20:
21: # Are IP address the same?
22: If ($Result)
23: {
24: # Yes, both IP addresses are the same, do nothing, only write a log entry
25: $row = $TimeStamp + "," + $CheckedIP + "," + $CurrentIP + ",nochange"
26: }
27: else
28: {
29: # External IP is not equal to IP set in externalPCoIPURL
30: # Changing the externalPCoIPURL
31: #Update-ConnectionBroker -broker_id "ConnectionServerNameHere" -externalPCoIPURL $CheckedIP
32: Update-ConnectionBroker -broker_id "SecurityServerNameHere" -externalPCoIPURL $CheckedIP
33:
34: # Check if it was succesful
35: $NewSettings = Get-ConnectionBroker
36: $row = $TimeStamp + "," + $CheckedIP + "," + $CurrentIP + "," + $NewSettings.externalPCoIPURL
37: }
38: $row | Out-File -FilePath "c:\logging\check-ip.log" –Append

This was a great start but utilized an external web page for resolving the IP address. In my case, the security boundary is between internal network zones (industrial control system network on the inside, enterprise WAN on the outside) and an external service will provide me the IP address for the internet facing firewall instead of the outside interface of the firewall.

DNS on the untrusted side of this firewall will resolve the external URL to the proper external IP address. As such I replaced line 12 of this script with:

$CheckedIP = [System.Net.DNS]::GetHostEntry("ExternalSecurityServerNameHere").AddressList[0].IPAddressTo String

I also needed to uncomment line 2 when running the script inside task scheduler

With the script in place I then added a scheduled task that triggers once per day and repeats once an hour. The task launches "C:\Windows\system32\WindowsPowerShell\powershell.exe" with arguments "& 'C:\Program Files\VMware\VMware View\Server\extras\PowerShell\check-ip.ps1'"

This will keep the security server settings in VDM in sync with the upstream DNS server at least. The firewall handles keeping the DNS name synced with its outside interface IP.