Implementing SSL with PowerShell for existing SharePoint 2013 farm – Part 5

Up to part 4 of this series I have completed the web applications and IIS sites to work with SSL certificates. In this 5th part I will provide you the script to change the Host Named Site Collections (HNSC) to use HTTPS.

In part 1 I have described that I have a mixture of HNSC’s and Path based site collections. In order to have everything accessible on HTTPS, only the HNSCs need to be changed.

The HNSCs are recognized by the URLs ending with company.com. Everything else is a path based site collection.

The script

$webappUrl = "https://hostingwebapp.company.com"

$webapp = Get-SPWebApplication $webappUrl


$webapp.Sites | % {
    
    $hnsc = $_
    if( $hnsc ) {
    
        $oldUrl = $hnsc.Url
        if($oldUrl.EndsWith(".company.com") ) {

            Write-Host Renaming $hnsc.Url -NoNewline

            $newUrl = $oldUrl.Replace("http://", "https://")
            $uri = New-Object System.Uri($newUrl)
            $hnsc.Rename($uri)
            
            Write-Host -f Green " [Done]"
        }
    }
        
}


That should do it.

Running the script

Before running the script, the site collections look like this:

image

When I run the script, the result looks like this:

image

Share