Tuesday, May 5, 2015

Add Pingdom Probe Server IPs to IIS using PowerShell

I'm by no means a PowerShell script person.  It's pretty useful though, so after an hour or so along with a bunch of internet searches, I came up with a script to add the list of Pingdom Probe servers to my IIS allow list.

This script will parse the list of Pingdom probe servers (https://support.pingdom.com/Knowledgebase/Article/View/61/0/probe-servers) and add the given IPs to the IP Restriction Allow list for an IIS application.

$applicationPath = "Default Web Site/ApplicationName"

$doc = [xml](Invoke-WebRequest -URI "https://www.pingdom.com/rss/probe_servers.xml")
$ns = New-Object Xml.XmlNamespaceManager $doc.NameTable
$ns.AddNamespace("pingdom", "http://www.pingdom.com/ns/PingdomRSSNamespace")
$nodes = $doc.SelectNodes("/rss/channel/item/pingdom:ip", $ns)

$currentIPList = Get-WebConfiguration system.webServer/security/ipSecurity -Location $applicationPath -PSPath IIS:\

foreach($ip in $nodes)
    $exists = $currentIPList.Collection | where { $_.ipAddress -eq $ip.'#text' }

    if (!$exists)
        $configEntry = @{allowed="true"; ipAddress=$ip.'#text'}
        Add-WebConfiguration  system.webServer/security/ipSecurity -Location $applicationPath -Value $configEntry -PSPath IIS:\
        "Added: " + $ip.'#text'