Solved

Restrict IIS websites by IP

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
OfficeMate Freezes on login or does not load after login credentials are input.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

930 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

16 Experts available now in Live!

Get 1:1 Help Now