2016-11-09 47 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" } 
} 
相關問題