![](/img/trans.png)
[英]powershell use of where-object within script function not working
[英]powershell variable into where-object does not return data
我正在編寫一個腳本,以最終通過FriendlyName檢查服務器塊中是否有證書,然后返回並在確認后將其刪除。 現在,我只是想讓最初的檢查工作。 當前它不返回任何數據。 有人可以幫忙嗎?
$ContentsPath = "C:\Servers.txt"
$Servers = Get-Content $ContentsPath
$CertDeletionFile = "C:\CertsDeleted.csv"
$Today = Get-Date
$Certificate = Read-Host -Prompt "What certificate would you like to
REMOVE?"
write-host $Certificate
function findCert {
param ([string]$Certificate)
Invoke-Command -ComputerName $Servers -ScriptBlock {Get-Childitem -Path
Cert:LocalMachine\My | where {$_.friendlyname -eq $Certificate } | Select-
Object -Property FriendlyName }
}
findCert
正如Mathias R. Jessen所評論的那樣,您的findcert函數需要將證書名稱作為參數,並且在調用它時不會傳遞任何內容,因此它將無法正常運行。
您還嘗試在調用命令內的遠程計算機上使用本地計算機變量$Certificate
,並且遠程計算機無法在遠程處理中訪問該變量。
我用$using:
重寫了它,它是一種語法,它告訴PS通過遠程會話發送該值,並使用重命名的變量,以便更清楚地了解哪個部分正在訪問哪些變量:
$ContentsPath = 'C:\Servers.txt'
$Servers = Get-Content -LiteralPath $ContentsPath
$CertDeletionFile = 'C:\CertsDeleted.csv'
$Today = Get-Date
$typedCertificateName = Read-Host -Prompt "What certificate would you like to
REMOVE?"
write-host $typedCertificateName
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
}
}
findCert -Certificate $typedCertificateName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.