代码之家  ›  专栏  ›  技术社区  ›  Nuisance

无法使用变量获取已安装的角色

  •  0
  • Nuisance  · 技术社区  · 11 月前

    我有下面的代码,我不明白为什么它不返回结果。

    $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功能,我会得到预期的输出。

    1 回复  |  直到 11 月前
        1
  •  -1
  •   Pulkownik    11 月前

    因为远程会话不知道本地变量。

     $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