iis 6 - Using Powershell to Manipulate IP Restrictions on IIsWebVirtualDir -


having trouble using powershell manipulate ip restrictions on iiswebvirtualdir (virtual directories).

however, have code in vbs, simple matter :)

code in vbs:

 sub add2iprlist(websiteadsi, strip2add, strip2addsubnet)     set webrootobj = getobject(websiteadsi) '"iis://localhost/w3svc/2/root/testvdir"     set ipsecobj = webrootobj.ipsecurity     if(ipsecobj.grantbydefault)then         iplist = ipsecobj.ipdeny     else         iplist = ipsecobj.ipgrant     end if      redim preserve iplist (ubound(iplist)+1)     'resize local copy of iplist array currentsize+1     iplist(ubound(iplist))=strip2add&","&strip2addsubnet     'add entry end of array       if(ipsecobj.grantbydefault)then         ipsecobj.ipdeny = iplist     else         ipsecobj.ipgrant = iplist     end if      webrootobj.ipsecurity = ipsecobj     webrootobj.setinfo        'apply setttings on server.     set ipsecobj = nothing     set webrootobj = nothing     end sub 

attempt 1 in powershell: object returns, of strange type.

ps c:\> $vdir=[adsi]"iis://localhost/w3svc/2/root/testvdir";([adsi]$vdir).ipsecurity; system.__comobject 

attempt 2 in powershell: object doesnt return

ps c:\> $vdir = get-wmiobject -namespace 'root\microsoftiisv2' -class iiswebvirtualdir |where ($_.name).contains("testvdir")};$vdir.ipsecurity; ps c:\>  

anyone know how either 1) deal system.__comobject when using adsi in powershell or 2) have idea how work ipsecurity object in iis6 via wmi provider in powershell?

additionally:

i found way pull , modify iisipsecuritysetting object associated w3svc/2/root/testvdir using following code.

param([string]$computer, [string]$w3svcpath, [string]$strip2add, [string]$strip2addsubnet) <# $w3svcpath = "w3svc/2/root/testvdir" #>; $ipsecurity = get-wmiobject -authentication packetprivacy -class iisipsecuritysetting -computername $computer -namespace 'root\microsoftiisv2' | {($_.name).equals("$w3svcpath")}; if($ipsecurity.grantbydefault){$gd="deny"}else{$gd="grant"} if($ipsecurity.grantbydefault){$iplist=$ipsecurity.ipdeny;}else{$iplist=$ipsecurity.ipgrant;}; "ipsecurity.grantbydefault=$gd($iplist)"; $iplist=$iplist+"$strip2add, $strip2addsubnet"; if($ipsecurity.grantbydefault){$ipsecurity.ipdeny=$iplist;}else{$ipsecurity.ipgrant=$iplist;}; if($ipsecurity.grantbydefault){$iplist=$ipsecurity.ipdeny;}else{$iplist=$ipsecurity.ipgrant;}; "($iplist)"; 

i cant seem find way set object metabase apply change. in vbs ipsecurity object referenced directly within webrootobj , .setinfo() function used. however, we're going wmi object class directly, , references set within object itself, cant seem find function set within iisipsecuritysettings class.

since cant find reference ipsecurity property/object within webrootobj when using "attempt 2 in powershell" above, uses wmi, i'm not sure direction move in next.

any thoughts?

this can tricky doable using system.directoryservices. i'll give 2 examples, 1 set value of grantbydefault true or false, other show how add ip addresses ipdeny or ipgrant list.

1. set grantbydefault value

$iisobject = new-object system.directoryservices.directoryentry("iis://localhost/w3svc/2/root/testvdir") $ipsec = $iisobject.properties["ipsecurity"].value  # need pass values 1 element object arrays [object[]] $grantbydefault = @() $grantbydefault += , $false            # <<< we're setting false  $ipsec.gettype().invokemember("grantbydefault", $bindingflags, $null, $ipsec, $grantbydefault);  $iisobject.properties["ipsecurity"].value = $ipsec $iisobject.commitchanges() 

2. add ip address ipdeny or ipgrant lists

$iisobject = new-object system.directoryservices.directoryentry("iis://localhost/w3svc/2/root/testvdir") $ipsec = $iisobject.properties["ipsecurity"].value $bindingflags = [reflection.bindingflags] "public, instance, getproperty" $isgrantbydefault = $ipsec.gettype().invokemember("grantbydefault", $bindingflags, $null, $ipsec, $null);  # set iplist need first if($isgrantbydefault) {     $iplist = $ipsec.gettype().invokemember("ipdeny", $bindingflags, $null, $ipsec, $null); } else {     $iplist = $ipsec.gettype().invokemember("ipgrant", $bindingflags, $null, $ipsec, $null); }  # add single computer list: $iplist = $iplist + "10.0.0.1, 255.255.255.255"  # important, need pass object array of 1 element containing our iplist array [object[]] $iparray = @() $iparray += , $iplist  # update $bindingflags = [reflection.bindingflags] "public, instance, setproperty" if($isgrantbydefault) {     $iplist = $ipsec.gettype().invokemember("ipdeny", $bindingflags, $null, $ipsec, $iparray); } else {     $iplist = $ipsec.gettype().invokemember("ipgrant", $bindingflags, $null, $ipsec, $iparray); }  $iisobject.properties["ipsecurity"].value = $ipsec $iisobject.commitchanges() 

this tested powershell 2.0 on windows 2003.

hopefully not late save day.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -