{"id":1548,"date":"2015-01-04T23:58:31","date_gmt":"2015-01-04T15:58:31","guid":{"rendered":"http:\/\/www.jhouseconsulting.com\/?p=1548"},"modified":"2015-01-05T00:14:32","modified_gmt":"2015-01-04T16:14:32","slug":"script-to-import-and-bind-a-certificate-to-the-default-web-site","status":"publish","type":"post","link":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/2015\/01\/04\/script-to-import-and-bind-a-certificate-to-the-default-web-site-1548","title":{"rendered":"Script to Import and Bind a Certificate to the Default Web Site"},"content":{"rendered":"<p><a href=\"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-content\/uploads\/2015\/01\/ssl-certificate.png\"><img decoding=\"async\" class=\"alignleft wp-image-1549 size-full\" title=\"SSL Certificate\" src=\"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-content\/uploads\/2015\/01\/ssl-certificate.png\" alt=\"SSL Certificate\" width=\"194\" height=\"206\" \/><\/a>This Powershell script will import and bind a certificate to the Default\u00a0Web Site.\u00a0I use this script for\u00a0Citrix StoreFront and Director deployments, but it&#8217;s written to be very flexible and versatile so can be used for other tasks.<\/p>\n<p>The original idea came from scripts written by <a href=\"http:\/\/www.albaek.org\/automatic-installation-of-citrix-storefront-2-6\/\" target=\"_blank\">Thomas Albaek<\/a> and\u00a0<a href=\"https:\/\/jeromequief.wordpress.com\/2014\/06\/11\/storefront-2-5-unattended-install-and-config\/\" target=\"_blank\">Jerome Quief<\/a>\u00a0for Citrix StoreFront.<\/p>\n<p>The way I&#8217;ve written this script it will actually remove\u00a0any existing certificate bindings first, which is really handy for pushing out updated certificates as I found that other scripts written to do this task would fail when run more than once.<!--more--><\/p>\n<p>Review\u00a0the documentation within the script to understand the syntax.<\/p>\n<p>Here is the <a  data-e-Disable-Page-Transition=\"true\" class=\"download-link\" title=\"\" href=\"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/download\/1552\/?tmstv=1776914471\" rel=\"nofollow\" id=\"download-link-1552\" data-redirect=\"false\" >\n\tInstall-Certificate.ps1\t(1552 downloads\t)\n<\/a>\n script:<\/p>\n<pre class=\"brush: powershell; auto-links: false; title: ; toolbar: false; notranslate\" title=\"\">\r\n&lt;#\r\n  This script will import and bind a certificate to the Default\r\n  Web Site for use with Citrix StoreFront, etc.\r\n\r\n  The original idea came from scripts written by Thomas Albaek and\r\n  Jerome Quief:\r\n  - http:\/\/www.albaek.org\/automatic-installation-of-citrix-storefront-2-6\/\r\n  - https:\/\/jeromequief.wordpress.com\/2014\/06\/11\/storefront-2-5-unattended-install-and-config\/\r\n\r\n  I have enhanced and modernized it. You can either pass parameters\r\n  to it, or hardcode them. The nice thing about this script is that\r\n  it can also be used to remove\/update certificates.\r\n\r\n  Syntax examples:\r\n\r\n    Using hardcoded variables:\r\n      Install-Certificate.ps1\r\n\r\n    Passing parameters:\r\n      Install-Certificate.ps1 -PFXPath:&quot;.\\star_jhouseconsulting_com.pfx&quot; -PFXPassword:&quot;notT3LL1ngu&quot; -CertSubject:&quot;CN=*.jhouseconsulting.com&quot;\r\n\r\n    The ExcludeLocalServerCert is optional, and is forced to $True\r\n    if left off. You really never want this set to false, especially\r\n    if using a wildcard certificate. It's there mainly for flexibility.\r\n\r\n    If the password contains a $ sign, you must escape it with the `\r\n    character.\r\n\r\n  Script Name: Install-Certificate.ps1\r\n  Release 1.0\r\n  Written by Jeremy@jhouseconsulting.com 21st December 2014\r\n\r\n  Note: This script has been tested thoroughly on Windows 2012R2\r\n        (IIS 8.5). Due to the cmdlets used I cannot guarantee full\r\n        backward compatibility.\r\n\r\n  A log file will either be written to %windir%\\Temp or to the\r\n  %LogPath% Task Sequence variable if running from an SCCM\\MDT\r\n  Task.\r\n\r\n#&gt;\r\n\r\n#-------------------------------------------------------------\r\nparam(&#x5B;String]$PFXPath,&#x5B;String]$PFXPassword,&#x5B;String]$CertSubject,&#x5B;switch]$ExcludeLocalServerCert)\r\n\r\n# Set Powershell Compatibility Mode\r\nSet-StrictMode -Version 2.0\r\n\r\n$ScriptPath = {Split-Path $MyInvocation.ScriptName}\r\n\r\nif (&#x5B;String]::IsNullOrEmpty($PFXPath)) {\r\n  $PFXPath = $(&amp;$ScriptPath) + &quot;\\star_jhouseconsulting_com.pfx&quot;\r\n}\r\n\r\nif (&#x5B;String]::IsNullOrEmpty($PFXPassword)) {\r\n  $PFXPassword = &quot;notT3LL1ngu&quot;\r\n}\r\n\r\nif (&#x5B;String]::IsNullOrEmpty($CertSubject)) {\r\n  $CertSubject = &quot;CN=*.jhouseconsulting.com&quot;\r\n}\r\n\r\nif (!($ExcludeLocalServerCert.IsPresent)) {\r\n  $ExcludeLocalServerCert = $True\r\n}\r\n\r\n# Set to the Web Site\r\n$sitename = &quot;Default Web Site&quot;\r\n\r\n# Set to the Port number\r\n$port = 443\r\n\r\n#-------------------------------------------------------------\r\n\r\nFunction IsTaskSequence() {\r\n  # This code was taken from a discussion on the CodePlex PowerShell\r\n  # App Deployment Toolkit site. It was posted by mmashwani.\r\n  Try {\r\n      &#x5B;__ComObject]$SMSTSEnvironment = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction 'SilentlyContinue' -ErrorVariable SMSTSEnvironmentErr\r\n  }\r\n  Catch {\r\n  }\r\n  If ($SMSTSEnvironmentErr) {\r\n    Write-Verbose &quot;Unable to load ComObject &#x5B;Microsoft.SMS.TSEnvironment]. Therefore, script is not currently running from an MDT or SCCM Task Sequence.&quot;\r\n    Return $false\r\n  }\r\n  ElseIf ($null -ne $SMSTSEnvironment) {\r\n    Write-Verbose &quot;Successfully loaded ComObject &#x5B;Microsoft.SMS.TSEnvironment]. Therefore, script is currently running from an MDT or SCCM Task Sequence.&quot;\r\n    Return $true\r\n  }\r\n}\r\n\r\n#-------------------------------------------------------------\r\n\r\n$invalidChars = &#x5B;io.path]::GetInvalidFileNamechars()\r\n$datestampforfilename = ((Get-Date -format s).ToString() -replace &quot;&#x5B;$invalidChars]&quot;,&quot;-&quot;)\r\n\r\n# Get the script path\r\n$ScriptPath = {Split-Path $MyInvocation.ScriptName}\r\n$ScriptName = &#x5B;System.IO.Path]::GetFilenameWithoutExtension($MyInvocation.MyCommand.Path.ToString())\r\n$Logfile = &quot;$ScriptName-$($datestampforfilename).txt&quot;\r\n$logPath = &quot;$($env:windir)\\Temp&quot;\r\n\r\nIf (IsTaskSequence) {\r\n  $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment\r\n  $logPath = $tsenv.Value(&quot;LogPath&quot;)\r\n\r\n  $UserDomain = &#x5B;System.Text.Encoding]::ASCII.GetString(&#x5B;System.Convert]::FromBase64String($tsenv.Value(&quot;UserDomain&quot;)))\r\n  $UserID = &#x5B;System.Text.Encoding]::ASCII.GetString(&#x5B;System.Convert]::FromBase64String($tsenv.Value(&quot;UserID&quot;)))\r\n  $UserPassword = &#x5B;System.Text.Encoding]::ASCII.GetString(&#x5B;System.Convert]::FromBase64String($tsenv.Value(&quot;UserPassword&quot;)))\r\n}\r\n\r\n$logfile = &quot;$logPath\\$Logfile&quot;\r\n\r\n# Start the logging\r\nStart-Transcript $logFile\r\nWrite-Output &quot;Logging to $logFile&quot;\r\n\r\n#-------------------------------------------------------------\r\n\r\nWrite-Output &quot;Start Certificate Installation&quot;\r\n\r\nWrite-Output &quot;Loading the Web Administration Module&quot;\r\ntry{\r\n    Import-Module webadministration\r\n}\r\ncatch{\r\n    Write-Output &quot;Failed to load the Web Administration Module&quot;\r\n}\r\n\r\nWrite-Output &quot;Deleting existing certificate from Store&quot;\r\ntry{\r\n    $cert = Get-ChildItem cert:\\LocalMachine\\MY | Where-Object {$_.subject -like &quot;$CertSubject*&quot; -AND $_.Subject -notmatch &quot;CN=$env:COMPUTERNAME&quot;}\r\n    $thumbprint = $cert.Thumbprint.ToString()\r\n    If (Test-Path &quot;cert:\\localmachine\\my\\$thumbprint&quot;) {\r\n      Remove-Item -Path cert:\\localmachine\\my\\$thumbprint -DeleteKey\r\n    }\r\n}\r\ncatch{\r\n    Write-Output &quot;Unable to delete existing certificate from store&quot;\r\n}\r\n\r\nWrite-Output &quot;Running certutil to import certificate into Store&quot;\r\ntry{\r\n    $ImportError = certutil.exe -f -importpfx -p $PFXPassword $PFXPath\r\n}\r\ncatch{\r\n    Write-Output &quot;certutil failed to import certificate: $ImportError&quot;\r\n}\r\n\r\nWrite-Output &quot;Locating the cert in the Store&quot;\r\ntry{\r\n    If ($ExcludeLocalServerCert) {\r\n      $cert = Get-ChildItem cert:\\LocalMachine\\MY | Where-Object {$_.subject -like &quot;$CertSubject*&quot; -AND $_.Subject -notmatch &quot;CN=$env:COMPUTERNAME&quot;}\r\n    } Else {\r\n      $cert = Get-ChildItem cert:\\LocalMachine\\My | Where-Object {$_.subject -like &quot;$CertSubject*&quot;}\r\n    }\r\n    $thumbprint = $cert.Thumbprint.ToString()\r\n    Write-Output $cert\r\n}\r\ncatch{\r\n    Write-Output &quot;Unable to locate cert in certificate store&quot;\r\n}\r\n\r\nWrite-Output &quot;Removing any existing binding from the site and SSLBindings store&quot;\r\ntry{\r\n  # Remove existing binding form site\r\n  if ($null -ne (Get-WebBinding -Name $sitename | where-object {$_.protocol -eq &quot;https&quot;})) {\r\n    $RemoveWebBinding = Remove-WebBinding -Name $sitename -Port $Port -Protocol &quot;https&quot;\r\n    Write-Output $RemoveWebBinding\r\n  }\r\n  # Remove existing binding in SSLBindings store\r\n  If (Test-Path &quot;IIS:\\SslBindings&#92;&#48;.0.0.0!$port&quot;) {\r\n    $RemoveSSLBinding = Remove-Item -path &quot;IIS:\\SSLBindings&#92;&#48;.0.0.0!$port&quot;\r\n    Write-Output $RemoveSSLBinding\r\n  }\r\n}\r\ncatch{\r\n    Write-Output &quot;Unable to remove existing binding&quot;\r\n}\r\n\r\nWrite-Output &quot;Bind your certificate to IIS HTTPS listener&quot;\r\ntry{\r\n  $NewWebBinding = New-WebBinding -Name $sitename -Port $Port -Protocol &quot;https&quot;\r\n  Write-Output $NewWebBinding\r\n  $AddSSLCertToWebBinding = (Get-WebBinding $sitename -Port $Port -Protocol &quot;https&quot;).AddSslCertificate($thumbprint, &quot;MY&quot;)\r\n  Write-Output $AddSSLCertToWebBinding\r\n}\r\ncatch{\r\n    Write-Output &quot;Unable to bind cert&quot;\r\n}\r\n\r\nWrite-Output &quot;Completed Certificate Installation&quot;\r\n\r\n# Stop logging\r\nStop-Transcript\r\n<\/pre>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Powershell script will import and bind a certificate to the Default\u00a0Web Site.\u00a0I use this script for\u00a0Citrix StoreFront and Director deployments, but it&#8217;s written to be very flexible and versatile so can be used for other tasks. The original idea came from scripts written by Thomas Albaek and\u00a0Jerome Quief\u00a0for Citrix StoreFront. The way I&#8217;ve written &#8230; <a title=\"Script to Import and Bind a Certificate to the Default Web Site\" class=\"read-more\" href=\"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/2015\/01\/04\/script-to-import-and-bind-a-certificate-to-the-default-web-site-1548\" aria-label=\"Read more about Script to Import and Bind a Certificate to the Default Web Site\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[14,410,401,5],"tags":[403,405,416,406,408,404,402,341,340,407],"class_list":["post-1548","post","type-post","status-publish","format-standard","hentry","category-citrix","category-citrix-director","category-citrix-storefront","category-scripting","tag-bind","tag-certutil","tag-citrix","tag-default-web-site","tag-director","tag-iis","tag-install-certificate","tag-posh","tag-powershell","tag-storefront"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/1548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/comments?post=1548"}],"version-history":[{"count":6,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/1548\/revisions"}],"predecessor-version":[{"id":1558,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/1548\/revisions\/1558"}],"wp:attachment":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/media?parent=1548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/categories?post=1548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/tags?post=1548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}