I am trying to write a script to check a remote server for a certificate by FriendlyName. Once this is returned I want to confirm a removal of this cert. Currently the code below returns "Provider execution stopped because the provider does not support this operation." Why does the provider not work during the Remove-Item cmd but works earlier in the script when I use Select-Object?
$Logfile = "C:\Cert_Deletions_$(get-date -Format MMddyyyy).log"
Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}
#$TimeStamp = Get-Date;
LogWrite "Starting Cert Deletion Job $(get-date)";
$ContentsPath = 'C:\Servers.txt'
$Servers = "Server01"
$CertDeletionFile = 'C:\CertsDeleted.csv'
$Today = Get-Date
$typedCertificateName = Read-Host -Prompt "What certificate would you like
to REMOVE?"
LogWrite "What Certificate would you like to REMOVE?"
function findCert {
param ([string]$Certificate)
Invoke-Command -ComputerName $Servers -ScriptBlock {
Get-Childitem -Path Cert:LocalMachine\My |
where-Object {$_.friendlyname -eq $using:Certificate } |
Select-Object -Property FriendlyName
}
}
#line break
"`n"
Write-host "The following servers were found to hold the
$typedCertificateName certificate:"
LogWrite "The following servers were found to hold the $typedCertificateName
certificate:"
#line break
"`n"
$LocatedOn = findCert -Certificate $typedCertificateName
$LocatedOn
LogWrite $LocatedOn
"`n"
Write-host "Do you want to delete all certificates for $typedCertificateName
??" -ForegroundColor Red
LogWrite "Do you want to delete all certificates for $typedCertificateName
??"
$Readhost = Read-Host " ( y / n ) "
Switch ($ReadHost)
{
Y {Write-host "Yes, Deleting Now!!!" -ForegroundColor Yellow;
$Choice=$true}
N {Write-Host "No, Do NOT DELETE" -ForegroundColor Red; $Choice=$false}
Default {Write-Host "Default, Do Not Delete"; $Choice=$false}
}
If ($Readhost -eq 'y' -or 'Y') {
Foreach ($Server in $Servers) {
Invoke-Command -ComputerName $Server -ScriptBlock {
try {
Get-Childitem -Path Cert:LocalMachine\My |
where-Object {$_.friendlyname -eq
$using:typedCertificateName} |
Remove-Item -ErrorAction Stop
Write-host "$using:typedCertificateName has been deleted on
$Server."
#LogWrite "$using:typedCertificateName has been deleted on
$Server."
}
catch
{
write-host $error
}
}
}
}
Here is an example of how to delete certificates on PowerShell v2 where the Remove-Item cmdlet doesn't work for certificate deletions. The script will first do the deletion and then verify that the deletion occurred. Feel free to modify for your purposes: