Iterate over all the bindings configured in IIS using powershell

I am looking for a way to go through all the bind settings already configured in my IIS.

I use this to work with IIS in Powershell:

Import-Module WebAdministration 

Until now, I could get the information I needed:

 $Websites = Get-ChildItem IIS:\Sites 

My $ Websites array is filled correctly and with the following command ...

 $Websites[2] 

.. I get this result:

 Name ID State Physical Path Bindings ---- -- ----- ------------- -------------- WebPage3 5 D:\Web\Page3 http *:80:WebPage3 https *:443:WebPage3 

Now here is the part I have difficulty with:

I want to check if the binding is correct. For this I need a binding. I tried:

 foreach ($site in $Websites) { $site = $Websites[0] $site | select-string "http" } 

Debugging this code shows me that $ Site does not contain what I expected: "Microsoft.IIs.PowerShell.Framework.ConfigurationElement". Currently, I don't know how to explicitly get the binding information for something like this (inside the foreach loop):

  if ($site.name -eq "WebPage3" -and $site.Port -eq "80") { #website is ok } else { #remove all current binding #add correct binding } 

Thank you for your help!


Decision:

 Import-Module WebAdministration $Websites = Get-ChildItem IIS:\Sites foreach ($Site in $Websites) { $Binding = $Site.bindings [string]$BindingInfo = $Binding.Collection [string]$IP = $BindingInfo.SubString($BindingInfo.IndexOf(" "),$BindingInfo.IndexOf(":")-$BindingInfo.IndexOf(" ")) [string]$Port = $BindingInfo.SubString($BindingInfo.IndexOf(":")+1,$BindingInfo.LastIndexOf(":")-$BindingInfo.IndexOf(":")-1) Write-Host "Binding info for" $Site.name " - IP:"$IP", Port:"$Port if ($Site.enabledProtocols -eq "http") { #DO CHECKS HERE } elseif($site.enabledProtocols -eq "https") { #DO CHECKS HERE } } 
+7
source share
3 answers

I do not know exactly what you are trying to do, but I will try. I see that you are referring to $Websites[2] , which is webPage3. You can do it as follows:

 $site = $websites | Where-object { $_.Name -eq 'WebPage3' } 

Then, when you look at $site.Bindings , you will realize that you need a Collection member:

 $site.bindings.Collection 

On my machine, this returns:

 protocol bindingInformation -------- ------------------ http *:80: net.tcp 808:* net.pipe * net.msmq localhost msmq.formatname localhost https *:443: 

And the test might look like this:

 $is80 = [bool]($site.bindings.Collection | ? { $_.bindingInformation -eq '*:80:' }) if ($is80) { #website is ok } else { #remove all current binding #add correct binding } 

I sent the contents of the Collection to the pipeline and filter only objects where the bindingInformation property is equal to the desired value (change it). Then I dropped it on [bool] . This will return $true if there is otherwise the item you need, $false .

+7
source

I found that if the site had several bindings, then if I needed script access to individual parts of the bindings, otherwise I only got the first binding. To get them all, I need the script to expand as shown below:

 Import-Module WebAdministration $Websites = Get-ChildItem IIS:\Sites foreach ($Site in $Websites) { $Binding = $Site.bindings [string]$BindingInfo = $Binding.Collection [string[]]$Bindings = $BindingInfo.Split(" ") $i = 0 $header = "" Do{ Write-Output ("Site :- " + $Site.name + " <" + $Site.id +">") Write-Output ("Protocol:- " + $Bindings[($i)]) [string[]]$Bindings2 = $Bindings[($i+1)].Split(":") Write-Output ("IP :- " + $Bindings2[0]) Write-Output ("Port :- " + $Bindings2[1]) Write-Output ("Header :- " + $Bindings2[2]) $i=$i+2 } while ($i -lt ($bindings.count)) } 
+1
source

I had something similar to the last answer, but this fixes the HTTPS sites and adds a bit more useful information.

 Import-Module WebAdministration $hostname = hostname $Websites = Get-ChildItem IIS:\Sites $date = (Get-Date).ToString('MMddyyyy') foreach ($Site in $Websites) { $Binding = $Site.bindings [string]$BindingInfo = $Binding.Collection [string[]]$Bindings = $BindingInfo.Split(" ")#[0] $i = 0 $status = $site.state $path = $site.PhysicalPath $fullName = $site.name $state = ($site.name -split "-")[0] $Collection = ($site.name -split "-")[1] $status = $site.State $anon = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/AnonymousAuthentication -Name Enabled -PSPath IIS:\sites -Location $site.name | select-object Value $basic = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/BasicAuthentication -Name Enabled -PSPath IIS:\ -location $site.name | select-object Value Do{ if( $Bindings[($i)] -notlike "sslFlags=*"){ [string[]]$Bindings2 = $Bindings[($i+1)].Split(":") $obj = New-Object PSObject $obj | Add-Member Date $Date $obj | Add-Member Host $hostname $obj | Add-Member State $state $obj | Add-Member Collection $Collection $obj | Add-Member SiteName $Site.name $obj | Add-Member SiteID $site.id $obj | Add-member Path $site.physicalPath $obj | Add-Member Protocol $Bindings[($i)] $obj | Add-Member Port $Bindings2[1] $obj | Add-Member Header $Bindings2[2] $obj | Add-member AuthAnon $Anon.value $obj | Add-member AuthBasic $basic.value $obj | Add-member Status $status $obj #take this out if you want to save to csv| export-csv "c:\temp\$date-$hostname.csv" -Append -notypeinformation $i=$i+2 } else{$i=$i+1} } while ($i -lt ($bindings.count)) } 
0
source

All Articles