我正在尝试使用 powershell 控制 IIS 应用程序中的绑定。我想使用脚本创建一个同时包含 http 和 https 绑定的站点。
这是我迄今为止所拥有的:
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$hostname,
[Parameter(Mandatory=$True,Position=2)]
[string]$installPath,
[Parameter(Mandatory=$False,Position=3)]
[string]$ip
)
Import-Module WebAdministration
$appPoolName = $hostname + 'Pool'
$port = 80
$hostRecord = $hostname+'.example.com'
$bindings = @{protocol="http";bindingInformation=$ip + ":"+ $port + ":" + $hostRecord}
New-Item IIS:\AppPools\$appPoolName
Set-ItemProperty IIS:\AppPools\$appPoolName managedRuntimeVersion v4.0
New-Item IIS:\Sites\$hostname -Bindings $bindings -PhysicalPath $installPath
Set-ItemProperty IIS:\Sites\$hostname -Name applicationPool -Value $appPoolName
Run Code Online (Sandbox Code Playgroud)
如何将绑定添加到我的$bindings变量/使用其他一些机制来实现我的目标?
小智 25
您可以使用 New-WebBinding:http ://technet.microsoft.com/en-us/library/ee790567.aspx
例如
IIS:\>New-WebBinding -Name "Default Web Site" -IPAddress "*" -Port 80 -HostHeader TestSite
Run Code Online (Sandbox Code Playgroud)
Pro*_*gle 13
我经历了尝试向站点添加 https 绑定的过程,这可能非常痛苦。有很多方法可以完成每一步,每一步都有陷阱。我将留下最终解决方案,希望有人会发现它有用。
此解决方案假定您已安装 IIS 并定义了网站。出于本文的目的,请致电网站 sample.contoso.com。假设您在 sample.contoso.com.pfx 文件中有一个要使用的证书。
第一步是从文件中导入证书。
$certPwd = ConvertTo-SecureString -String "password" -Force -AsPlainText
$webServerCert = Import-PfxCertificate -FilePath c:\some\folder\sample.contoso.com.pfx -CertStoreLocation Cert:\LocalMachine\My -Password $certPwd
Run Code Online (Sandbox Code Playgroud)
如果这足够了,那就太好了。在某些情况下可能是。但是,对我来说,这使证书无法正确访问私钥。当我将证书添加到绑定时,这导致了一个 powershell 错误“指定的登录会话不存在。它可能已经被终止”(请参阅后面的步骤)。因此,下一步是修复私钥的 ACL。
$privateKeyFilename = $webServerCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
$privateKeyFullPath = "c:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\"+$privateKeyFilename
$aclRule = "SYSTEM", "Full", "Allow"
$aclEntry = New-Object System.Security.AccessControl.FileSystemAccessRule $aclRule
$privateKeyAcl = (Get-Item $privateKeyFullPath).GetAccessControl("Access")
$privateKeyAcl.AddAccessRule($aclEntry)
Set-Acl $privateKeyFullPath $privateKeyAcl
Run Code Online (Sandbox Code Playgroud)
如果私钥不是从包含文件夹继承的,这将允许本地系统具有对私钥的完全访问权限。
如果你想获得一个已经安装的证书,你需要它的哈希值,并且可以像这样使用 Get-Item 检索它:
$webServerCert = get-item Cert:\LocalMachine\My\XFX2DX02779XFD1F6F4X8435A5X26ED2X8DEFX95
Run Code Online (Sandbox Code Playgroud)
下一步是创建绑定。
New-WebBinding -Name sample.contoso.com -IPAddress * -Port 443 -Protocol "https"
Run Code Online (Sandbox Code Playgroud)
重要的是要注意“https”区分大小写。如果您改用“HTTPS”,则会得到完全不同的绑定结果。
此绑定尚未附加证书,因此最后一步是附加证书。如果证书被正确信任且安全性正确,则此步骤应该会成功。如果证书有任何问题,这可能会很挑剔。
$bind = Get-WebBinding -Name $webSiteDNSName -Protocol https
$bind.AddSslCertificate($webServerCert.GetCertHashString(), "my")
Run Code Online (Sandbox Code Playgroud)
如果此操作失败并显示有关登录会话不存在的消息,则证书可能存在问题。查看事件查看器以获取更多详细信息。在我的努力中,我在安全日志中发现了事件 5061。当它失败时,它显示 OpenKey 失败,错误为 80090016(密钥集不存在)。失败是因为 SYSTEM 无法访问私钥。
这足以让我创建 https 绑定。http 绑定是使用 New-WebSite cmdlet 的副产品。如果它不是免费提供的,我认为使用 New-WebBinding cmdlet 创建端口 80 绑定并不是一个挑战。
| 归档时间: |
|
| 查看次数: |
51728 次 |
| 最近记录: |