2016-11-09 46 views
0

如果符合条件,证书即被删除,但文本“The cert is being removed”未显示出来。预期写入条件在脚本中不起作用

请告知

$pc = '.' 
$cert_store = 'My' 
write "The is the cert store we are working with : $cert_store" 
$store = New-Object system.security.cryptography.X509Certificates.X509Store ("My","LocalMachine") #LocalMachine could also be LocalUser 
$store.Open('ReadWrite') 
write "opening the store for editing" 
## Find all certs that have an Issuer of old CA 
$certs = $store.Certificates | Where { $_.Issuer -eq 'CN=DomainRootCA, DC=Corporate, DC=Local' } 
if ($certs -ne $null) 
{ 
    ## Remove all the certs it finds 
    $certs | foreach { $store.Remove($_) | write "The cert is being removed" } 
} 
+0

如果更改此行$ certs | foreach {$ store.Remove($ _)|写“证书正在被删除”}到这行$ certs | foreach {$ store.Remove($ _);写“证书正在被删除”}(意思是将管道改为分号) – DVT

+0

感谢您的建议@DVT它完美的工作! – NobleMan

+0

@DVT回答 –

回答

2

我相信这是由于有来自拆下输出什么。如果将管道(|)更改为分号(;),则可以使用。

$pc = '.' 
$cert_store = 'My' 
write "The is the cert store we are working with : $cert_store" 
$store = New-Object system.security.cryptography.X509Certificates.X509Store ("My","LocalMachine") #LocalMachine could also be LocalUser 
$store.Open('ReadWrite') 
write "opening the store for editing" 
## Find all certs that have an Issuer of old CA 
$certs = $store.Certificates | Where { $_.Issuer -eq 'CN=DomainRootCA, DC=Corporate, DC=Local' } 
if ($certs -ne $null) 
{ 
    ## Remove all the certs it finds 
    $certs | foreach { $store.Remove($_) ; write "The cert is being removed" } 
} 
相关问题