Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 我想在证书的现有指纹与站点匹配时导入SSL证书_Powershell_Ssl Certificate - Fatal编程技术网

Powershell 我想在证书的现有指纹与站点匹配时导入SSL证书

Powershell 我想在证书的现有指纹与站点匹配时导入SSL证书,powershell,ssl-certificate,Powershell,Ssl Certificate,我编写了一个PS脚本,其中会将证书导入现有站点到IIS服务器,但我需要一个脚本,其中会将我拥有的证书指纹与本地计算机存储中的证书指纹匹配,如果指纹匹配,则将该证书导入存储,如果没有,请不要导入证书 例如,我有一个指纹为XXXXXX的pfx文件,需要检查脚本,检查我的机器或任何远程服务器中是否有与上述相同的指纹,然后需要替换我拥有的证书或将其导入该位置 代码 Clear-Host $certPath = 'C:\TEMP\Sample.pfx' $CertificatePassword = 'X

我编写了一个PS脚本,其中会将证书导入现有站点到IIS服务器,但我需要一个脚本,其中会将我拥有的证书指纹与本地计算机存储中的证书指纹匹配,如果指纹匹配,则将该证书导入存储,如果没有,请不要导入证书

例如,我有一个指纹为XXXXXX的pfx文件,需要检查脚本,检查我的机器或任何远程服务器中是否有与上述相同的指纹,然后需要替换我拥有的证书或将其导入该位置

代码

Clear-Host

$certPath = 'C:\TEMP\Sample.pfx'
$CertificatePassword = 'XXXXXX'
$SiteName = "SampleTest"
$HostName = "Sitebinding.com"
$SiteFolder = Join-Path -Path 'C:\inetpub\wwwroot' -ChildPath $SiteName

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 $applicationPool}

Write-Host 'Bind certificate with Thumbprint' $certThumbprint
#$obj = get-webconfiguration "//sites/site[@name='$SiteName']"
$obj = Get-WebBinding $SiteName -Port 443
#$binding = $obj.bindings.Collection[0]
#$method = $binding.Methods["AddSslCertificate"]
$method = $obj.Methods["AddSslCertificate"]
$methodInstance = $method.CreateInstance()
$methodInstance.Input.SetAttributeValue("certificateHash", $certThumbprint)
$methodInstance.Input.SetAttributeValue("certificateStoreName", $certStore)
$methodInstance.Execute()```

Thanks In Advance.

您似乎可以执行
if
语句,其中包括查询
Cert:
PSDrive
指纹。然后仅根据条件更新绑定

if (Get-ChildItem Cert:\LocalMachine -Recurse | Where Thumbprint -eq $certThumbprint) {
    # Update Binding
}