• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1025
  • Last Modified:

Restrict IIS websites by IP


Running Windows 2008 Server/ IIS 7. I need to restrict access to a particular site from the web server - the restriction should be based on IP. There are about 30 IP addresses only we want to allow, they are in a file named IP_Safe.csv

I've seen this article here: http://www.hrzdata.com/node/46

Which shows how to do this manually.

But we have about 30 servers, is there any way to automate this via Powershell?
1 Solution
Here is a quick how to :


You can simply loop the command indicated in a powershell script.
Meir RivkinFull stack Software EngineerCommented:
in your csv file, have each ip in different line, without headers
create servers.txt file contains all server names in each line.

[Object[]] $ipArray = @()
gc c:\temp\IP_Safe.csv | %{
	$ipArray += $_

gc c:\temp\servers.txt | %{
	$server = $_
	#get iis object
	$iisObject = new-object System.DirectoryServices.DirectoryEntry("IIS://" + $server + "/w3svc/1/root")
	#get iis security property object
	$ipSec = $iisObject.Properties["IPSecurity"].Value
	#update binding flags to GetProperty
	$bindingFlags = [Reflection.BindingFlags] "Public, Instance, GetProperty"
	#get currently deny list
	$ipList = $ipSec.GetType().InvokeMember("IPDeny", $bindingFlags, $null, $ipSec, $null);
	#add to new ip list
	$ipArray += , $ipList
	#update binding flags to SetProperty
	$bindingFlags = [Reflection.BindingFlags] "Public, Instance, SetProperty"
	#update deny ip list
	$ipList = $ipSec.GetType().InvokeMember("IPDeny", $bindingFlags, $null, $ipSec, $ipArray);
	#set iis security property object
	$iisObject.Properties["IPSecurity"].Value = $ipSec
	#commit changes

Open in new window

based on this post:
Using Powershell to Manipulate IP Restrictions on IIsWebVirtualDir
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now