Add Binding To IIS – PowerShell

Hi Guys, today we are going to disuses about how to add the binding to IIS using Powershell. In most case, during the deployments, the one of the painful activity is adding binding for new sites. So this below posts will help those members to one step towards of Automation.

The New-WebBinding cmdlet adds a new binding to an existing website.

#Assigining the URL which need to bind
$_WebsiteUrl = "www.dotnet-helpers.com"
#Name of the WEB SITE name created under Sites Folder(IIS)
$_TargetIISWebsite ="Default Web site"
#It has a method IsNullOrEmpty() which returns true if the passed string is null or empty.
if([string]::IsNullOrWhiteSpace($_TargetIISWebsite))
{     
  write-host "website not created." 
}   
else    
{       
  #TRIM: Removes all leading and trailing white-space characters from the current String object
  $WebsiteUrl = $_WebsiteUrl.Trim(' ')
}
foreach ($Website in $_WebsiteUrl)
{
  #We are adding the SSL binding to the Default Web Site using New-WebBinding cmdlets
  New-WebBinding -Name $web -IPAddress "*" -Port 80 -protocol http -HostHeader $Website -sslflags
  New-WebBinding -Name $web -IPAddress "*" -Port 443 -protocol https -HostHeader $Website -sslflags  
  write-host $Website "Binding created successfully on" $_TargetIISWebsite   
}
}

Explanation:

The New-WebBinding cmdlet adds a new binding to an existing website.

New-WebBinding -Name $web -IPAddress “*” -Port 80 -protocol http -HostHeader $Website -sslflags 0

-Name The name of the Web site on which the new binding is created.
-IPAddress The IP address of the new binding.
-Port The port used for the binding.
-Protocol The protocol to be used for the Web binding (usually HTTP, HTTPS, or FTP).
-HostHeader The host header of the new binding.
-SslFlags Indicates what type of certificate OR certificate storage the new website supports. Only the below values are valid:

0 (Regular certificate in Windows certificate storage),
1 (SNI certificate),
2 (central certificate store),
3 (SNI certificate in central certificate store).

 

 

What do you think?

I hope you have idea of how to add the binding to IIS using Powershell Script. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

2 thoughts on “Add Binding To IIS – PowerShell”

  1. I have to move one site to another server which has around 500 site bindings. How can I achieve this. I am totally zero about the power shell script but knows c#.net. Any help is deeply appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.