Import pfx file into particular certificate store from command line

后端 未结 7 1361
时光取名叫无心
时光取名叫无心 2021-01-30 17:12

It\'s relatively easy to import a certificate into the user\'s personal store from a pfx file by using CertUtil:

certutil –f –p [certificate_password] –importpfx         


        
7条回答
  •  离开以前
    2021-01-30 17:39

    Here is the complete code, import pfx, add iis website, add ssl binding:

    $SiteName = "MySite"
    $HostName = "localhost"
    $CertificatePassword = '1234'
    $SiteFolder = Join-Path -Path 'C:\inetpub\wwwroot' -ChildPath $SiteName
    $certPath = 'c:\cert.pfx'
    
    
    Write-Host 'Import pfx certificate' $certPath
    $certRootStore = “LocalMachine”
    $certStore = "My"
    $pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
    $pfx.Import($certPath,$CertificatePassword,"Exportable,PersistKeySet") 
    $store = New-Object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore) 
    $store.Open('ReadWrite')
    $store.Add($pfx) 
    $store.Close() 
    $certThumbprint = $pfx.Thumbprint
    
    
    Write-Host 'Add website' $SiteName
    New-WebSite -Name $SiteName -PhysicalPath $SiteFolder -Force
    $IISSite = "IIS:\Sites\$SiteName"
    Set-ItemProperty $IISSite -name  Bindings -value @{protocol="https";bindingInformation="*:443:$HostName"}
    if($applicationPool) { Set-ItemProperty $IISSite -name  ApplicationPool -value $IISApplicationPool }
    
    
    Write-Host 'Bind certificate with Thumbprint' $certThumbprint
    $obj = get-webconfiguration "//sites/site[@name='$SiteName']"
    $binding = $obj.bindings.Collection[0]
    $method = $binding.Methods["AddSslCertificate"]
    $methodInstance = $method.CreateInstance()
    $methodInstance.Input.SetAttributeValue("certificateHash", $certThumbprint)
    $methodInstance.Input.SetAttributeValue("certificateStoreName", $certStore)
    $methodInstance.Execute()
    

提交回复
热议问题