0

我正在尝试将 SSL 证书绑定到我的站点。如果我在管理员模式下从 PowerShell ise 运行脚本,它可以正常工作,但是当我在管理员模式下从 PowerShell 7 运行时,它不会将证书绑定到站点。我在我的脚本中使用以下部分:

#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!"
        }
    }
}
4

0 回答 0