Using PowerShell to filter and sort IIS Binding info…

     In this post I want to talk about a few PowerShell commands for grabbing info about IIS bindings that utilize the Webadministration Module. When a server is running a lot of sites, sorting through the bindings can be a daunting task especially if you are looking for specific information like say what IP’s are bound to SSL/HTTPS or what sites are running FTP. The following commands can be used to pull binding information from IIS allowing for further sorting and manipulation to make this a much simpler task.

     First, make sure execution policy is set to allow the Import Module scripts to run. Open PowerShell and type the following commands:

# Check the current policy
PS C:\Users\Administrator> Get-ExecutionPolicy
Restricted

# Set the policy
PS C:\Users\Administrator> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is “Y”): y

# Verify the policy is set
PS C:\Users\Administrator> Get-ExecutionPolicy
RemoteSigned

Loading the PowerShell Modules

     Once the modules are loaded, they are only persistent for the session, so if you close PowerShell the modules will have to be reloaded.

     To load the modules into PowerShell:

Go to Start > All Programs > Administrative Tools > Windows PowerShell Modules

     Or

At a PowerShell Prompt type:

PS C:\Users\Administrator> Get-Module -ListAvailable | Import-Module

To add just the Webadministration module:

PS C:\Users\Administrator> Import-Module Webadministration

Getting All Binding information from IIS

     To view all sites by name along with ID, current state, physical path and binding information run the Get-ChildItem command against the IIS:\ Drive and the Sites Folder:

PS C:\Users\Administrator> Get-ChildItem -Path IIS:\Sites

gci_sites

Filter results using Where-Object:

PS C:\Users\Administrator> Get-ChildItem -Path IIS:\Sites | Where-Object {$_.ID -match “2”}

gci_sites2

Note: Using Where-Object against the bindings here does not return the expected results, try findstr for that…

Filter bindings by piping to findstr:

PS C:\Users\Administrator> Get-ChildItem -Path IIS:\Sites | findstr “https”

gci_sites3

Using Get-WebBinding

     Use Get-WebBinding to get just the protocol and binding information like IP, Port and host header.

PS C:\Users\Administrator> Get-WebBinding

webbinding

Use Where-Object to filter for specific protocols:

PS C:\Users\Administrator> Get-WebBinding | Where-Object {$_.protocol -match “https”}

webbinding2

Comments are closed.