Tuesday, 19 March 2013

IIS 7 IP restrictions with PowerShell

PowerShell can be used to configure many options in IIS, this is useful for repetitive tasks such as IP restrictions on websites. First import the IIS module into powershell, execution policy must at least be set to remoteSigned.

Import-Module WebAdministration

Then use Add-WebConfiguration to add individual IP's or a subnet at a website or a directory/virtual directory under it:

Add-WebConfiguration /system.webServer/security/ipSecurity -location 'test1' -value @{ipAddress='192.168.1.2';allowed='true'}

Add-WebConfiguration /system.webServer/security/ipSecurity -location 'test1/test2' -value @{ipAddress='192.168.1.2';allowed='true'}

Add-WebConfiguration /system.webServer/security/ipSecurity -location 'test1/test2' -value @{ipAddress='192.168.3.0'; subnetMask='255.255.255.0';allowed='true'}

Set-WebConfigurationProperty can be used to allow or deny unlisted IP addresses at a site or a directory/virtual directory under it:

Set-WebConfigurationProperty -Filter /system.webserver/security/ipsecurity -Name allowUnlisted -Value $false -Location 'test1/test2'

Set-WebConfigurationProperty -Filter /system.webserver/security/ipsecurity -Name allowUnlisted -Value $true -Location 'test1'

1 comment:

  1. Ok, that is good.
    But how do you remove inaccurate IP settings.

    ReplyDelete