I'm currently setting up auto scaling IIS webservers and need to automatically install and configure the following through a powershell script:

  • IIS
  • URLRewrite
  • Import SSL certificate
  • Configure a new website
  • Add new SSL bindings
  • Download my source code from a GIT repository



  • 3
    SO is not the right place for showcasing completed scripts, particularly not if you post them as questions. Please use a blog or forum for this instead. – Ansgar Wiechers Aug 13 '14 at 14:52
  • Sorry, thought it'd be as helpful as me asking the question, having got advice and providing the answer. I never asked the initial question, but it was something I came across different elements on stackoverflow about so thought it'd be helpful answered and more use than never posting at all. Would it be better if i marked it as correct or changed the first post to the question and then post own answer for solution? – Liam Wheldon Aug 13 '14 at 14:56
  • It's more or less accepted if you can make up an actual question and post an answer of your own that showcases the script. – Ansgar Wiechers Aug 13 '14 at 15:15
  • I've quickly reworded it to a question/answer setup, hopefully it'll help someone one day. Not many Google searches I search which don't end up directing me to a StockOverflow thread :-) – Liam Wheldon Aug 13 '14 at 15:37

I just thought I'd share a powershell script that I put together with you all as I came across a situation with AWS ELB where I needed to install IIS, URL rewrite, git and clone the repository.

echo "Installing web-webserver"
powershell.exe add-windowsfeature web-webserver -includeallsubfeature -logpath $env:temp\webserver_addrole.log 
echo "Installing web-mgmt-tools"
powershell.exe add-windowsfeature web-mgmt-tools -includeallsubfeature -logpath $env:temp\mgmttools_addrole.log

echo "Creating C:\inetpub\wwwroot\example.com\"
$TestApplicationroot = Test-Path C:\inetpub\wwwroot\example.com
if (! $TestApplicationroot) {
    mkdir C:\inetpub\wwwroot\example.com

echo "GIT: Installing Chocolatey"
echo "GIT: Installing Git"
cinst git
echo "GIT: Setting enviroment path"
echo "GIT: Installing poshgit"
cinst poshgit
echo "GIT: Installing UrlRewrite"
cinst UrlRewrite
echo "GIT: Installing git-credential-winstore"
cinst git-credential-winstore

.\CredMan.ps1 -AddCred -Target 'git:https://gitrespos.org' -User 'TestApplication' -Pass 'TestApplicationPassword'

echo "GIT: Cloning TestApplication1 code"
cd C:\inetpub\wwwroot\example.com\
git clone "https://gitrespos.org/Username/TestApplication1.git"

import-module webadministration

echo "Creating new website"
new-website -name "example.com" -port 80 -physicalpath c:\inetpub\wwwroot\example.com -ApplicationPool ".NET v4.5" -force 

Echo "Importing SSL certificate"
$mypwd = ConvertTo-SecureString -String "SSLCertificate password" -Force –AsPlainText
Import-PfxCertificate –FilePath .\certificate.pfx cert:\localMachine\my -Password $mypwd
New-WebBinding -Name "example.com" -IP "*" -Port 443 -Protocol https

echo "Assigning SSL certificate"
cd IIS:\SslBindings
$cert = Get-Item cert:\LocalMachine\My\THUMB-OF-SSL-CERTIFICATE
$cert |New-Item!443

echo "Adding application pools TestApplication1"
New-Item 'IIS:\Sites\example.com\TestApplication1' -physicalPath "C:\inetpub\wwwroot\example.com\TestApplication1" -type Application

echo "Removing Default Web Site"
remove-website -name "Default Web Site"
Start-Sleep -s 10
echo "Starting example.com website"
start-website -name "example.com"

You can download CredMan.ps1 from the following link http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde

You'll need to first find the Thumb of your certificate on a server by running the following in powershell and note down the Thumbprint as it'll be the same on every server you import the certificate to.:

get-ChildItem cert:\LocalMachine\My

I hope that this is of help to some of you as it's taken me days to come up with having hit different issues along the way.



Here is the complete code, import pfx, add iis website, add ssl binding:

$certPath = 'c:\cert.pfx'
$CertificatePassword = '1234'
$SiteName = "MySite"
$HostName = "localhost"
$SiteFolder = Join-Path -Path 'C:\inetpub\wwwroot' -ChildPath $SiteName

Write-Host 'Import pfx certificate' $certPath
$certRootStore = “LocalMachine”
$certStore = "My"
$pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certThumbprint = $pfx.Thumbprint

Write-Host 'Add website' $SiteName
New-WebSite -Name $SiteName -PhysicalPath $SiteFolder -Force
$IISSite = "IIS:\Sites\$SiteName"
Set-ItemProperty $IISSite -name  Bindings -value @{protocol="https";bindingInformation="*:443:$HostName"}
if($applicationPool) { Set-ItemProperty $IISSite -name  ApplicationPool -value $applicationPool}

Write-Host 'Bind certificate with Thumbprint' $certThumbprint
$obj = get-webconfiguration "//sites/site[@name='$SiteName']"
$binding = $obj.bindings.Collection[0]
$method = $binding.Methods["AddSslCertificate"]
$methodInstance = $method.CreateInstance()
$methodInstance.Input.SetAttributeValue("certificateHash", $certThumbprint)
$methodInstance.Input.SetAttributeValue("certificateStoreName", $certStore)
  • 3
    Thanks for this :) I made one change, since I had both http and https bindings, so the https binding was not always returned in Collection[0]. Replace the three lines starting with $obj, $binding, $method with these two lines: $obj = Get-WebBinding $SiteName -Port 443 $method = $obj.Methods["AddSslCertificate"] – cometfish Jan 3 '17 at 3:02

Check out the Carbon module (disclaimer: I am the owner/maintainer of Carbon). It has functions for doing the IIS and SSL configuration and installing MSIs. Sadly, you'll have to figure out Git yourself. (I recommend switching to Mercurial. :-))

Good luck!

Your Answer

