NobleMan
NobleMan

Reputation: 515

Expected Write condition is not working in script

The cert is removed as expected if the condition is met, but the text "The cert is being removed" is not showing up.

Please advise

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

Upvotes: 0

Views: 54

Answers (1)

DVT
DVT

Reputation: 3127

I believe this was due to there is nothing output from Remove. If you change the pipe (|) to semicolon (;), it works.

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

Upvotes: 2

Related Questions