As you aware large number of unused URLs in the servers will lead critical to maintenance during the maintenance activity so we need to remove Multiple bindings in IIS which not in use . In our project, my team identified a large number of un-used URLs (almost 500+ URLs) across many servers and requested to make clean up in all the servers. It’s very hard to clean up the URLs manually and it will lead to manual error like wrongly removing others URLs which is already in use and almost it will take more days to complete the cleanup. So we decide to make this activity automated instead of manual cleaning to Remove multiple bindings in IIS.
To resolve the above scenario, we created the PowerShell script to remove large URLs in a single execution. Here let we discuss the script in detail. To demonstrate this, you’ll first either need to RDP to the webserver directly and open up a PowerShell console or use PowerShell remoting to connect to a remote session.
STEP: #1
First, we need to ensure query my default website using the Get-Website cmdlet. It will Get configuration information for an IIS Web site. After execution of the below line, it will return the configuration information for the “Default Web Site”.
Get-Website -Name “$(‘Default Web Site’)”
STEP: #2
After the execution of the above script, now the website information will be available, the next step we need to find the bindings (URLs) based on our parameters/criteria.
As you probably already know you can have multiple bindings attached to a single site. Using Get-WebBinding cmdlet, you can get bindings on the specified IIS site. We can get the binding based on the parameters like -Protocol, -Port, -HostHeader,-IPAddress, etc., From the below script, you can get the binding that matching HostHeader with HTTP/HTTPS with port 80/443.
Get-WebBinding -Protocol “http” -Port 80 -HostHeader $siteURL
Get-WebBinding -Protocol “https” -Port 443 -HostHeader $siteURL
STEP: #3
Now finally, we need to removed the URLs with help of Remove-WebBinding cmdlet. The Remove-WebBinding cmdlet will remove a binding from an Internet Information Services (IIS) website.
Remove-WebBinding
Full code (to remove multiple bindings in IIS )
Reading the list of URLs from the txt file and looping to remove the bindings from the IIS website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
############################################################################## #Project : How to remove the IIS binding from server using PowerShell script. #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ############################################################################## #Get list of URLs from the Text file $siteURLs = Get-Content -path C:\Desktop\ToBeRemoveURLs_List.txt #looping the URLs list to remove one by one foreach($siteURL in $siteURLs) { Get-Website -Name "$('Default Web Site')" | Get-WebBinding -Protocol "http" -Port 80 -HostHeader $siteURL| Remove-WebBinding Get-Website -Name "$('Default Web Site')" | Get-WebBinding -Protocol "https" -Port 443 -HostHeader $siteURL | Remove-WebBinding } |
Leave A Comment