Search This Blog

Tuesday 10 September 2019

Maintain manually added IP rules when website is re-created

Maintain manually added IP rules when website is re-created


   
   
 #cache IP rules into variable so they can be re-added to the replacement site later on  
 if(Get-ChildItem -Path IIS:\Sites | Where-Object {$_.Name -eq $WebsiteName}){  
   $ipRules = Get-WebConfiguration system.webServer/security/ipSecurity/* -PSPath "IIS:/sites/$WebsiteName"  
 }  
   
   
   
 #re-create website. -Force causes the existing site to be overwritten and all IP rules lost, including any manually configured ones  
 New-Website -Name $WebsiteName -ApplicationPool $WebsiteName -PhysicalPath "c:\whatever" -HostHeader "MyWebsite" -Force  
   
 # ... configure website and transform web.config with any default IP rules  
   
   
   
 #get any default IP rules that have been added to the site via web.config transforms  
 $defaultIpRules = Get-WebConfiguration system.webServer/security/ipSecurity/* -PSPath "IIS:/sites/$WebsiteName"  
   
 #put any custom IP rules back into "new" site. Comparison avoids duplication between cached rules and default rules  
 if($ipRules) {  
   $rules = compare-object $iprules $defaultIpRules -PassThru -Property "ipAddress" | where {$_.sideindicator -eq "<="}  
   Add-WebConfiguration system.webServer/security/ipSecurity -PSPath "IIS:/sites/$WebsiteName" -Value $rules  
 }  

No comments:

Post a Comment