以编程方式添加IIS 8证书映射

时间:2014-01-28 17:10:54

标签: iis vbscript

我尝试了几种方法,但我还没有成功。

我尝试从Microsoft修改此脚本失败了。某处我找不到错误。有人能帮助我吗?

set arguments = WScript.Arguments
if (arguments.length < 3 or arguments.length > 4) then
    WScript.Echo("Usage certmap.vbs <.cer file name> <userName> <password> [site]")
    WScript.Quit(0)
end if

certName = arguments(0)
user = arguments(1)
password = arguments(2)
site = "Default Web Site"

if (arguments.length = 4) then
    site = arguments(3)
end if

const forReading = 1

set shell = CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
cer = ""

set f = fso.OpenTextFile(certName, forReading)
s = f.ReadLine()

if (s <> "-----BEGIN CERTIFICATE-----") then
    f.Close
    shell.Run "cmd /C certutil -encode -f " + certName + " certToMap64.cer", 0, true
    set f = fso.OpenTextFile("certToMap64.cer", forReading)
    s = f.ReadLine()
end if

do while f.AtEndOfStream <> true
    s = f.ReadLine
    if f.AtEndOfStream <> true then
        cer = cer + s
    end if
loop

f.Close

WScript.Echo cer

configPath = "MACHINE/WEBROOT/APPHOST/" + site
configSectionName = "system.webServer/security/authentication/iisClientCertificateMappingAuthentication"

set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
set iisCertMap = adminManager.GetAdminSection(configSectionName, configPath)

iisCertMap.Properties.Item("enabled").Value = "true"
iisCertMap.Properties.Item("oneToOneCertificateMappingsEnabled").Value = "true"

set oneToOneMappingsElement = iisCertMap.ChildElements.Item("oneToOneMappings")
set mapping = oneToOneMappingsElement.collection.CreateNewElement()

mapping.Properties.Item("certificate").Value = cer
mapping.Properties.Item("enabled").Value = "true"
mapping.Properties.Item("userName").Value = user
mapping.Properties.Item("password").Value = password

oneToOneMappingsElement.Collection.AddElement(mapping)
adminManager.CommitChanges()

在certmappings.vbs中复制代码并使用相应参数执行后,它会在最后一行显示错误消息:“Keyset不存在”。我想知道错误应该在那之前的某个地方,它会在提交完成时出现。

我正在使用Windows 2012 R2和IIS 8.5.9600.16384。这个脚本可以在IIS7中使用。

有什么想法吗?

提前多多感谢。

1 个答案:

答案 0 :(得分:0)

解决!

我通过使用IIS8附带的命令行实用程序找到了一种解决方法。

appcmd.exe set config "Default Web Site" -section:system.webserver/security/authentication/iisClientCertificateMappingAuthentication /+"onetoonemappings.[userName='midominio\jgrodrigo',password='P@ssw0rd',certificate='MIIJhzCCCG+gAwIBAgIQVOKUGessUOCW...

工作正常。希望这对有这样一个问题的下一个人有用。

干杯!