我有下面的代码,我不明白为什么它不返回结果。
$List = (Get-ADComputer -Filter 'OperatingSystem -like "*server*" -and enabled -eq "true"').name $Role = ('RDS-RD-Server').ToString() invoke-command -computername $List -scriptblock{ Get-WindowsFeature | Where-Object {$_.Name -eq $Role -and $_.installstate -eq "installed"} | select PsComputerName, Installstate } -ErrorAction Ignore
如果我使用“RDS RD Server”而不是使用变量运行get-windows功能,我会得到预期的输出。
因为远程会话不知道本地变量。
$List = (Get-ADComputer -Filter 'OperatingSystem -like "*server*" -and enabled -eq "true"').name $Role = ('RDS-RD-Server').ToString() invoke-command -computername $List -scriptblock{ Get-WindowsFeature | Where-Object {$_.Name -eq $Using:Role -and $_.installstate -eq "installed"} | select PsComputerName, Installstate } -ErrorAction Ignore
点击此处阅读更多信息 about_Remote_Variables