New-IISSiteBinding 返回绑定已存在,但绑定在 IIS 中不存在

New-IISSiteBinding 返回绑定已存在,但绑定在 IIS 中不存在

我正在尝试使用 PowerShell 将 IIS 绑定从 json 文件导入 IIS。但每当我运行以下命令时:

New-IISSiteBinding -Name "IIS Site Name" -BindingInformation *:443:actualwebsite.com -CertificateThumbPrint *actualthumbprint* -CertStoreLocation My -Protocol https

它返回:

New-IISSiteBinding : Web site binding '*:443:actualwebsite.com' already exists.
At line:1 char:1
+ New-IISSiteBinding -Name "IIS Site Name" -BindingInformation *:4 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-IISSiteBinding], ArgumentException
    + FullyQualifiedErrorId : InvalidArgument,Microsoft.IIS.Powershell.Commands.NewIISSiteBindingCommand

当我执行Remove-IISSiteBinding命令时它说

WARNING: Web site binding 'https *:443:actualwebsite.com' does not exist.

它也不会显示在 IIS 管理控制台中,使用 iisreset 重新启动 IIS 不会改变任何内容。当我执行完整的 PowerShell 脚本时,我收到返回的错误:

New-IISSiteBinding : Filename: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config Error: Cannot commit configuration changes because the file has changed on disk

完整的 PowerShell 脚本:

$backupfilepath = "C:\Backup\IISBindings.json"
$Sites = Get-Content -Raw $backupfilepath | ConvertFrom-Json 
$Sites | ForEach-Object {
$siteName = $_.Site;
$_.Bindings | ForEach-Object {
    $certStoreName = "Cert:\LocalMachine\" + $_.CertificateStoreName;
    $bindingInformation = $_.BindingInformation
    $certificateHash = $_.CertificateHash
  
    if (-not ([string]::IsNullOrEmpty($certificateHash))) {
        if ($null -ne (get-iissitebinding -Name $siteName | Where-Object { $_.bindinginformation -eq $bindingInformation })) {  
            Write-Host "This is a current binding, removing existing"  
            Remove-IISSiteBinding -Name $siteName -Protocol https -BindingInformation $bindingInformation

            Write-Host "Creating a new binding"
            New-IISSiteBinding -Name $siteName -BindingInformation $bindingInformation -CertificateThumbPrint $certificateHash -CertStoreLocation $certStoreName -Protocol https -Force
        }  
        else {  
            Write-Host "This is not a current binding"  
            New-IISSiteBinding -Name $siteName -BindingInformation $bindingInformation -CertificateThumbPrint $certificateHash -CertStoreLocation $certStoreName -Protocol https -Force
        }
    }
    else {
        if ($null -ne (get-iissitebinding -Name $siteName | Where-Object { $_.bindinginformation -eq $bindingInformation })) {  
            Write-Host "This is a current binding, removing existing"  
            Remove-IISSiteBinding -Name $siteName -Protocol http -BindingInformation $bindingInformation
            
            Write-Host "Creating a new binding"
            New-IISSiteBinding -Name $siteName -BindingInformation $bindingInformation -Protocol http -Force
        }  
        else {  
            Write-Host "This is not a current binding"  
            New-IISSiteBinding -Name $siteName -BindingInformation $bindingInformation -Protocol http -Force
        }
    } 
   

}

}

我该如何解决这个问题?

答案1

感谢@Cpt.Whale,我找到了Chris Meagher 的博客文章他解释说他使用 IISServerManager。切换到这个之后我的脚本就可以正常工作了!

使用博客文章我可以用以下内容替换我的每个循环:

$Sites | ForEach-Object {
$siteName = $_.Site;
$_.Bindings | ForEach-Object {
    $certStoreName = $_.CertificateStoreName;
    $bindingInformation = $_.BindingInformation
    $certificateHash = $_.CertificateHash
    $thumbprintBytes = HexToBytes $certificateHash
    
    $site = $manager.Sites[$siteName]
    if (-not ([string]::IsNullOrEmpty($certificateHash))) {
        $site.Bindings.Add($bindingInformation, $thumbprintBytes, $certificateStoreName, 0) | Out-Null
    } else {
        $site.Bindings.Add($bindingInformation, "http") | Out-Null
    }
}
}

$manager.CommitChanges()

相关内容