我想自动化 IIS 7.5 的SSL证书安装过程。首选方法是使用VBScript。我解决了这个问题,即创建一个新的HTTPS绑定并将正确的证书绑定到此绑定。
我实际上解决了这个问题,为我的网络服务器激活添加IIS管理脚本和工具角色,并使用如下脚本:
Set serverWebAdmin = GetObject("winmgmts:root\WebAdministration")
' EC8BCFF70983EA26BFEA087683329CB8C07366A5 is an certificate hash of the fake certificate
' that i obtain from the staging environment of Let's Encrypt
' "MY" is the name of certificate storage
serverWebAdmin.Get("SSLBinding").Create "*", 443,"EC8BCFF70983EA26BFEA087683329CB8C07366A5", "MY"
Set newBinding = serverWebAdmin.Get("BindingElement").SpawnInstance_
newBinding.BindingInformation = "*:443:"
newBinding.Protocol = "https"
Set issuedWebSite = serverWebAdmin.Get("Site.Name='sitename.com'")
webSiteBindings = issuedWebSite.Bindings
ReDim Preserve webSiteBindings(UBound(webSiteBindings) + 1)
Set webSiteBindings(UBound(webSiteBindings)) = newBinding
issuedWebSite.Bindings = webSiteBindings
Set pathResult = issuedWebSite.Put_
它运行良好,但在使用WMI管理服务器之前,我尝试使用(并展开一点){MSN} an example如何创建绑定。我在VBScript上做了例子,并添加了证书哈希和证书存储名称的声明(我也检查了这些属性,它们现在似乎可以设置它们。我还检查了一些开源项目的代码,如WinAcme - 用C#编写 - 它们使用相同的属性)。
所以我的代码看起来像这样(设置绑定属性的部分):
Set bindingElement1 = bindingsCollection.CreateNewElement("binding")
bindingElement1.Properties.Item("protocol").Value = "https"
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:"
bindingElement1.Properties.Item("certificateHash").Value = "EC8BCFF70983EA26BFEA087683329CB8C07366A5"
bindingElement1.Properties.Item("certificateStoreName").Value = "MY"
bindingsCollection.AddElement(bindingElement1)
adminManager.CommitChanges()
它的工作原理但是它只创建了绑定,并且没有为这个绑定添加好的证书。我的问题是通过前面的代码片段解决的,但我想理解:它是第二个代码片段错了吗?是否有可能以这种方式绑定好的证书?
先谢谢你。
答案 0 :(得分:0)
它不能与您的bindingElement1变体一起工作的原因仅仅是因为您不能将其添加到bindingCollection中,而是必须将其添加到方法中:
您已经拥有的第一部分:
Dim bindingElement1 As ConfigurationElement = bindingsCollection.CreateElement("binding")
bindingElement1("protocol") = "https"
bindingElement1("bindingInformation") = "192.168.1.1:443:contoso.com"
bindingsCollection.Add(bindingElement1)
之后,只需添加:
Dim method = bindingElement1.Methods.Item("AddSslCertificate").CreateInstance()
method.Input.Attributes.Item("certificateHash").Value = "EC8BCFF70983EA26BFEA087683329CB8C07366A5"
method.Input.Attributes.Item("certificateStoreName").Value = "MY"
method.Execute()
提交更改:
serverManager.CommitChanges()
因此总的来说,有些错误可能看起来像这样:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetApplicationHostConfiguration
Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "contoso")
If (siteElement Is Nothing) Then
MsgBox("Element not found!")
End If
Dim bindingsCollection As ConfigurationElementCollection = siteElement.GetCollection("bindings")
Dim bindingElement1 As ConfigurationElement = bindingsCollection.CreateElement("binding")
bindingElement1("protocol") = "https"
bindingElement1("bindingInformation") = "192.168.1.1:443:contoso.com"
Try
bindingsCollection.Add(bindingElement1)
Catch ex As Exception : MsgBox(ex.Message) : End Try
Dim method = bindingElement1.Methods.Item("AddSslCertificate").CreateInstance()
method.Input.Attributes.Item("certificateHash").Value = "EC8BCFF70983EA26BFEA087683329CB8C07366A5"
method.Input.Attributes.Item("certificateStoreName").Value = "MY"
Try
method.Execute()
Catch ex As Exception : MsgBox(ex.Message) : End Try
serverManager.CommitChanges()
End Sub
Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
For Each element As ConfigurationElement In collection
If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
Dim matches As Boolean = True
Dim i As Integer
For i = 0 To keyValues.Length - 1 Step 2
Dim o As Object = element.GetAttributeValue(keyValues(i))
Dim value As String = Nothing
If (Not (o) Is Nothing) Then
value = o.ToString
End If
If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
matches = False
Exit For
End If
Next
If matches Then
Return element
End If
End If
Next
Return Nothing
End Function