Skip to content

Set CTLStoreName for multiple IIS SSL-bindings with PowerShell

December 1, 2021

This is a supplement to:

Import-Module -Name WebAdministration;

$ctlStoreName = "ClientAuthIssuer";

$hostNamePorts = @(
	"site1.example.com:443",
	"site1-a.example.com:443",
	"site2.example.com:443",
	"site2-a.example.com:443"
);

$ipPorts = @(
	"0.0.0.0:443"
);

foreach($sslBinding in (Get-ChildItem -Path IIS:SSLBindings))
{
	$hostNamePort = "$($sslBinding.Host):$($sslBinding.Port)".ToLowerInvariant();
	$ipPort = "$($sslBinding.IPAddress):$($sslBinding.Port)".ToLowerInvariant();

	if($hostNamePorts.Contains($hostNamePort))
	{
		Write-Host "--------------------------------------------------------------------------------------------------------------------------------------";
		Write-Host "http delete sslcert hostnameport=$($hostNamePort);";
		netsh http delete sslcert hostnameport=$hostNamePort;
		Write-Host "add sslcert hostnameport=$($hostNamePort) certhash=$($sslBinding.Thumbprint) appid=""{$($sslBinding.ApplicationId)}"" certstore=$($sslBinding.Store) sslctlstorename=$($ctlStoreName);";
		netsh http add sslcert hostnameport=$hostNamePort certhash=$($sslBinding.Thumbprint) appid="{$($sslBinding.ApplicationId)}" certstore=$($sslBinding.Store) sslctlstorename=$ctlStoreName;
		Write-Host "--------------------------------------------------------------------------------------------------------------------------------------";
	}
	elseif($ipPorts.Contains($ipPort))
	{
		Write-Host "--------------------------------------------------------------------------------------------------------------------------------------";
		Write-Host "http delete sslcert ipport=$($ipPort);";
		netsh http delete sslcert ipport=$ipPort;
		Write-Host "add sslcert ipport=$($ipPort) certhash=$($sslBinding.Thumbprint) appid=""{$($sslBinding.ApplicationId)}"" sslctlstorename=$($ctlStoreName);";
		netsh http add sslcert ipport=$ipPort certhash=$($sslBinding.Thumbprint) appid="{$($sslBinding.ApplicationId)}" sslctlstorename=$ctlStoreName;
		Write-Host "--------------------------------------------------------------------------------------------------------------------------------------";
	}
}

Information

From → Web, Windows

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: