首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过powershell或vbscript以编程方式将现有的ssl证书分配给iis6中的网站

通过powershell或vbscript以编程方式将现有的ssl证书分配给iis6中的网站
EN

Stack Overflow用户
提问于 2011-01-03 15:38:53
回答 3查看 4.3K关注 0票数 4

我有下面的powershell脚本,它在IIS6中创建了一个新网站:

https://github.com/dagda1/iis6/blob/master/create-site.ps1

有人知道我如何为网站分配一个现有的ssl证书吗?

我知道我可以像这样使用adsutil.vbs设置端口号:

cscript adsutil.vbs set w3svc/xxx/securebindings ":443:somewhere.com"

但是,当涉及到分配现有的ssl证书时,我感到很大的空白。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-01-04 02:30:59

我想这就是你想要的:

代码语言:javascript
复制
$w3svc = "W3SVC/566412209"     # <-- W3SVC/[iis number]
$pfxPath = "c:\ssl\myssl.pfx"
$pfxPassword = "password123"   # Whatever the certificate file's password is

$certMgr = New-Object -ComObject IIS.CertObj
$certMgr.ServerName = [System.Environment]::MachineName
$certMgr.InstanceName = $w3svc
$certMgr.Import($pfxPath, $pfxPassword, $true, $true)

您还可以创建一个.NET互操作程序集(通过向C:\WINDOWS\system32\inetsrv\certobj.dll添加COM引用或使用tlbimp.exe),以便您可以与PowerShell和.NET项目一起使用:

代码语言:javascript
复制
[void][reflection.assembly]::loadfrom("Interop.CERTOBJLib.dll")
$certMgr = new-object -typeName CERTOBJLib.IISCertObjClass
$certMgr.ServerName = [System.Environment]::MachineName
$certMgr.InstanceName = $w3svc
$certMgr.Import($pfxPath, $pfxPassword, $true, $true)

您仍然必须像前面所做的那样分别设置SSL端口绑定。

IIS.CertObj上的博士们在这里:

使用IISCertObj (IIS6.0)管理服务器证书

票数 5
EN

Stack Overflow用户

发布于 2011-06-22 13:11:40

由于这个问题已经得到了回答,我将在一个函数中提供我自己的口味。这相当于使用GUI打开网站和“分配现有证书”。

代码语言:javascript
复制
Function ImportSSLCertificateToWebsite{
    param([string]$pfxPath, [string]$pfxPassword, [string]$siteName, [string]$SSLIPPort);
    <# This script is deigned to run locally on the destination box; however it can be run remotely via Invoke-Command using a PSSession #>;
    <# $pfxPath can be a local or remote location "c:\somesite.pfx" or "\\someserver\share\somesite.pfx" #>;
    <# SSLIPPort must look like: "10.5.100.2:443" or ":443", will be split out below #>;
    $SSLIPPortTemp=$SSLIPPort.Split(":");
    $SSLIP=[string]$SSLIPPortTemp[0];
    $SSLPort=[string]$SSLIPPortTemp[1];
    $iisWebSite = Get-WmiObject -Namespace 'root\MicrosoftIISv2' -Class IISWebServerSetting -Filter "ServerComment = '$siteName'";
    $secureBindings=[System.Management.ManagementBaseObject[]]$iisWebSite.SecureBindings;
    $secureBindings[0].IP="$SSLIP";
    $secureBindings[0].PORT="$SSLPort"+":"; <# must look like "443:" #>;
    $iisWebSite.SecureBindings=[System.Management.ManagementBaseObject[]]$secureBindings;
    $iisWebSite.SetInfo;
    $iisWebsite.Put();
    <# to output the SecureBindings object for confirmation changes were made #>; foreach($a in [System.Management.ManagementBaseObject[]]$iisWebSite.SecureBindings){$a};
    $certMgr = New-Object -ComObject IIS.CertObj -ErrorAction SilentlyContinue;
    $certMgr.InstanceName = [string]$iisWebSite.Name;
    $certMgr.Import($pfxPath,$pfxPassword,$true,$true);
}
ImportSSLCertificateToWebsite "\\secure101\Certs\PFX\somesite.yourdomain.com.pfx" "ThePasswordForThePFX" "somesite.yourdomain.com" ":443"
票数 2
EN

Stack Overflow用户

发布于 2011-02-02 17:51:23

设置一些变量:

代码语言:javascript
复制
${HostName} = "mydomain.com"
${IPAddressString} = "127.0.0.1"
${Port} = 43
${CertificateThumbprint} = Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.Subject -like "CN=*${HostName}*" } | Select-Object -expand Thumbprint

然后运行netsh或httpcfg。

代码语言:javascript
复制
netsh http add sslcert "ipport=${IPAddressString}:${Port}" "certhash=${CertificateThumbprint}" "appid={4dc3e181-e14b-4a21-b022-59fc669b0914}"

代码语言:javascript
复制
httpcfg set ssl -i ${IPAddressString}:${Port} -h ${CertificateThumbprint} -g "{4dc3e181-e14b-4a21-b022-59fc669b0914}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4585809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档