【发布时间】:2019-01-01 14:45:18
【问题描述】:
我正在尝试编写一个脚本来通过 FriendlyName 检查远程服务器的证书。一旦返回,我想确认删除此证书。 目前,下面的代码返回“提供程序执行已停止,因为提供程序不支持此操作。”为什么提供程序在 Remove-Item cmd 期间不工作,但在我使用 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
}
}
}
}
【问题讨论】:
-
哪一行出现错误?
-
这意味着您正在使用 PowerShell 2。
Remove-Item在 PowerShell 3 之前不适用于cert:提供程序。从这里的 cmets - stackoverflow.com/a/37229338/478656 - 和这里的文档 - @ 987654322@。您已标记此 Powershell v4,但您的远程服务器可能仅在 v2 上? -
运行
Invoke-Command -Computername <server> -Scriptblock {$PSVersionTable.psversion}检查远程服务器的powershell版本 -
@TessellatingHeckler 你是对的。远程服务器正在使用 PSv2。本地服务器正在运行 PSv4。感谢您提供 PSv2 脚本。我越来越近了,但仍然需要一些帮助。我目前收到以下错误:详细:打开证书存储 [Cert:\LocalMachine\My]。详细:在 [ReadWrite] 中打开证书存储 [Cert:\LocalMachine\My] 详细:获取证书存储中的证书列表。详细:发现带有 FriendlyName [*.me.com] 的证书。
-
也感谢@Drew。返回消息从先前的评论 VERBOSE 继续:发现的证书 FriendlyName [*.me.com] 与指定的证书友好名称 [cert.you.org] 不匹配。详细:在此计算机上的证书存储区 [Cert:\LocalMachine\My] 中未发现 802.1x UniCERT 证书。跳过步骤以验证是否删除了相应的证书。
标签: powershell powershell-4.0 powershell-provider