Function powershell变量进入对象不返回数据的位置

Function powershell变量进入对象不返回数据的位置,function,powershell,Function,Powershell,我正在编写一个脚本,最终通过FriendlyName检查服务器块中的证书,然后在确认后返回并删除它们。现在我只是想让最初的检查生效。目前它没有返回任何数据。有人能帮忙吗 $ContentsPath = "C:\Servers.txt" $Servers = Get-Content $ContentsPath $CertDeletionFile = "C:\CertsDeleted.csv" $Today = Get-Date $Certificate = Read-Host -Prompt "

我正在编写一个脚本,最终通过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函数需要一个证书名作为参数,并且在调用它时没有传递任何内容,因此它无法正常运行

您还试图在远程计算机上的invoke命令中使用本地计算机变量$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

findCert->findCert-Certificate$Certificate在最后一行-否则findCert函数会将friendlyName与空StringTessellatingEckler进行比较,我尝试使用语法查找$using,结果是:,这似乎不是您使用的。你能告诉我在哪里可以找到关于这个解决方案的更多信息,因为我还没有完全了解它吗?谢谢。我很高兴:关于使用:我刚找到这个;显然,它被称为使用范围修饰符,并记录在这里——在使用局部变量部分