I am trying to bind the SSL certificate to my site. If I run the script from PowerShell ise in admin mode it works fine however, when I run from PowerShell 7 in admin mode, it does not bind the certificate to the site. I am using the below section in my script:
#Add SSL Certificate
if(($Protocol -eq "https") -and ($Port) -and ($AddConfiguration))
{
$newWebBindingCmd = "New-WebBinding -Name '$Name' -Protocol 'https' -Port '$port' -SslFlags 1"
$getWebBindingCmd = "Get-WebBinding -Name '$Name' -Port '$port'"
if($IPAddress)
{
$newWebBindingCmd += " -IPAddress '$IpAddress'"
$getWebBindingCmd += " -IPAddress '$IpAddress'"
}
If($hostname)
{
$newWebBindingCmd += " -HostHeader '$hostname'"
}
if (-not([string]::IsNullOrEmpty($CertificateKey)))
{
$cert = Get-ChildItem 'Cert:\LocalMachine\My' | where {($_.Subject -like "*$CertificateKey*") -or ($_.Thumbprint -like "*$CertificateKey*")};
if($cert -ne $null)
{
write-host 'Certificate Found';
$CommandLines = "Import-Module WebAdministration;$newWebBindingCmd;"
$createhttpsbinding = Execute-CommandLines -Commandlines $CommandLines.ToString();
if($createhttpsbinding -eq $true)
{
write-host 'https binding created successfully';
}
$CommandLines = "Import-Module WebAdministration;
`$httpsbinding = $getWebBindingCmd -Protocol 'https';
if(`$httpsbinding)
{
`$httpsbinding.AddSslCertificate(`$cert.GetCertHashString(), 'my');
write-host 'Added SslCertificate successfully';
}";
$addSslCertificate = Execute-CommandLines -Commandlines $CommandLines.ToString();
if($addSslCertificate -eq $true)
{
$CommandLines = "Import-Module WebAdministration;$getWebBindingCmd -Protocol 'http' | Remove-WebBinding;"
write-host 'SslCertificate installed. Removing http binding..';
}
else
{
$CommandLines = "Import-Module WebAdministration;$getWebBindingCmd -Protocol 'https' | Remove-WebBinding;"
write-host 'SslCertificate could not be installed. Removing https binding..';
}
$result = Execute-CommandLines -Commandlines $CommandLines.ToString()
if($result -eq $true)
{
Log -color Green "Binding Properties set successfully for WebService '$Name'"
}
else
{
Log -color Red "Binding Properties could not be set for WebService '$Name'"
}
}
else
{
Log -color Red "Certificate with CertificateKey '$CertificateKey' is not found!"
}
}
}