Solved

Restrict IIS websites by IP

Posted on 2013-01-27
2
992 Views
Last Modified: 2013-02-27
HI

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?
0
Comment
Question by:neil4933
2 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 38824221
Here is a quick how to :


http://technet.microsoft.com/en-us/library/cc733090%28v=ws.10%29.aspx


You can simply loop the command indicated in a powershell script.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38824260
in your csv file, have each ip in different line, without headers
create servers.txt file contains all server names in each line.

cls
[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
	$iisObject.CommitChanges()
}

Open in new window


based on this post:
Using Powershell to Manipulate IP Restrictions on IIsWebVirtualDir
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question