我试图控制绑定在一个IIS应用程序使用PowerShell。 我想用脚本创build一个包含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
如何添加绑定到我的$bindingsvariables/使用其他机制来实现我的目标?
您可以使用New-WebBinding: http ://technet.microsoft.com/en-us/library/ee790567.aspx
例如
IIS:\>New-WebBinding -Name "Default Web Site" -IPAddress "*" -Port 80 -HostHeader TestSite
我经历了尝试添加一个https绑定到一个网站的过程,这可能是非常痛苦的。 有很多方法可以完成每个步骤,每个步骤都有缺陷。 我将留下最后的解决scheme,希望有人会发现它有用。
此解决scheme假定您已安装IIS并定义了一个Web站点。 为了本文的目的调用站点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
如果这是足够的,那将是很好的。 在某些情况下,可能是。 但是,对于我来说,这使得证书无法正确访问私钥。 这导致了一个PowerShell错误“指定的login会话不存在,它可能已经被终止”,当我去证书添加到绑定(见后面的步骤)。 所以,下一步是修复私钥的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
这将允许本地系统完全访问私钥,如果这不是从包含文件夹inheritance。
如果你想获得一个已经安装的证书,你需要它的散列,并可以像Get一样检索它:
$webServerCert = get-item Cert:\LocalMachine\My\XFX2DX02779XFD1F6F4X8435A5X26ED2X8DEFX95
下一步是创build绑定。
New-WebBinding -Name sample.contoso.com -IPAddress * -Port 443 -Protocol "https"
注意“https”区分大小写很重要。 如果您使用“HTTPS”,您会得到一个非常不同的绑定结果。
该绑定还没有附加证书,所以最后一步是附加证书。 如果证书正确可信且安全性正确,这一步应该是成功的。 但是,如果证书有任何问题,它可以是挑剔的。
$bind = Get-WebBinding -Name $webSiteDNSName -Protocol https $bind.AddSslCertificate($webServerCert.GetCertHashString(), "my")
如果失败并且有关login会话的消息不存在,则证书可能有问题。 查看事件查看器获取更多详细信息。 在我的努力过程中,我在安全日志中发现了事件5061。 当它失败时,它显示OpenKey失败,80090016(键集不存在)。 而失败是因为系统没有访问私钥。
这足以让我创buildhttps绑定。 http绑定是使用New-WebSite cmdlet的副产品。 如果它不是免费的,我没有发现创build与New-WebBinding cmdlet的端口80绑定是一个挑战。
我想你以下是:
$bindings = @( @{protocol="http";bindingInformation=$ip + ":"+ $port + ":" + $hostRecord}, @{protocol="https";bindingInformation=$ip + ":"+ $port + ":" + $hostRecord} )
基本上你需要传递一个绑定数组。更有用的信息在这里 – ( http://blogs.iis.net/jeonghwan/iis-powershell-user-guide-comparing-representative-iis-ui-tasks )
(编辑:修正数组语法中的错字 – 无关的逗号)