2

Basically my issue comes down to this, I need to run code on a remote server using the ServerManager Module, but return the output to the local shell.

Here is the code run on WKS01:

Enter-PsSession SVR01
Import-Module ServerManager
$Roles = @(Get-WindowsFeature | Where {$_.installed -eq $true})

Now I need to use the data in $Roles on WKS01. Is there a way to do this? Am I going about this wrong?

4

2 回答 2

3

That is what -ArgumentList is for.

You can pass arguments to a remote session using this parameter. Here is an example:

$procName = "powershell"
Invoke-Command -ComputerName (get-content c:\scripts\servers.txt) `
   -ScriptBlock {param ($Name) Get-Process -Name $Name} `
   –ArgumentList $procName
于 2012-06-20T18:16:47.143 回答
2
$rs = New-PSSession -ComputerName SVR01
$Roles = Invoke-Command -Session $rs -ScriptBlock {
      Get-WindowsFeature | Where { $_.Installed }
}
Remove-PSSession $rs
于 2012-06-21T07:43:06.133 回答