簡體   English   中英

通過 SSL 證書和 Powershell 創建 https 綁定時出現“IIS:當該文件已存在時無法創建文件。Exception.Message”

[英]"IIS: Cannot create a file when that file already exists.Exception.Message" when creating a https binding through SSL certificate and powershell

我編寫了這個方法來使用已安裝的 SSL 證書並在 IIS 中啟用 https 綁定。 但是當我調用這個方法時,我從 Powershell 中出錯了這個錯誤。

IIS: Cannot create a file when that file already exists.Exception.Message

這是啟用綁定的功能。 我從 .json 文件中讀取的所有必需變量

function IIS-SSL-SETUP {
    
    $Global:iisStatus = $started
    try {
        $pwd = ConvertTo-SecureString -String $PFX_PASSWORD -Force -AsPlainText
        Import-PfxCertificate -FilePath $PFX_FILE_LOCATION Cert:\LocalMachine\My -Password $pwd
        $pfx.import($PFX_FILE_LOCATION, $PFX_PASSWORD, "Exportable,PersistKeySet") 
        $store = new-object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreName]::Root, "localmachine")
        $store.open("MaxAllowed") 
        $store.add($pfx) 
        $store.close()
        Import-Module WebAdministration
        Set-Location IIS:\
        if ($null -eq (Get-WebBinding "MyServer" | Where-Object { $_.bindingInformation -eq "*:$($IIS_SSS_HTTPS_PORT):" })) {
            New-WebBinding -Name "MyServer" -IP "*" -Port $IIS_SSS_HTTPS_PORT -Protocol https
            Get-WebBinding -Port $IIS_SSS_HTTP_PORT -Name "MyServer" | Remove-WebBinding
            cd SslBindings
            dir
            $pfx.Import($PFX_FILE_LOCATION, $PFX_PASSWORD, 'DefaultKeySet')
            $certThumbprint = "\LocalMachine\My\$($pfx.Thumbprint)"
            get-item Cert:$certThumbprint | new-item 0.0.0.0!$($IIS_SSS_HTTPS_PORT) //I am getting this error at this line
            $Global:iisStatus = "Passed"
        }
        else {
            $Global:iisStatus = "Failed"
            $Global:iisMsg = "Port $($IIS_SSS_HTTPS_PORT) is already in use, please mention some different port number in sslConfig.json."
            $Global:iisMsgColor = "Yellow"
        }
    }
    catch {
        $Global:iisStatus = "Failed"
        $Global:iisMsgColor = "Red"
        $Global:iisMsg = "IIS: $_.Exception.Message"
    }
}

@Mehul Parmar,正如你所問的,我已經刪除了我的評論並將其移到了答案中:

dir命令是否列出了某些內容? 在你的情況下它可能應該。 我的測試沒有結果並且運行良好,沒有錯誤。 您可以測試元素是否已經存在:

if (-not (Test-Path 0.0.0.0!$($IIS_SSS_HTTPS_PORT)))

"MyServer"是一個奇怪的網站名稱,不要忘記綁定綁定到站點而不是服務器。 還有一件事: Get-WebBinding -Port $IIS_SSS_HTTP_PORT -Name "MyServer" | Remove-WebBinding 如果未定義$IIS_SSS_HTTP_PORT Get-WebBinding -Port $IIS_SSS_HTTP_PORT -Name "MyServer" | Remove-WebBinding將刪除所有綁定。 也許你應該更喜歡這個:

Get-WebBinding -Protocol http -Name "MyServer" | Remove-WebBinding

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM