Solved

Restrict IIS websites by IP

Posted on 2013-01-27
2
980 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now