Link to home
Start Free TrialLog in
Avatar of Parity123
Parity123Flag for United States of America

asked on

Powershell : Remoting

Hello Experts,

How would I capture the output of running a remote command locally on my machine. I need to capture the output to a file locally on my machine. The output should be Computername, result. (There should be only 1 folder.)

$computers = gc c:\computers.txt
foreach ($computer in $computers) {
Invoke-Command -Computername  $computer - ScriptBlock { Get-ChildItem "c:\appoutput"}
}
Thanks for your assistance,
Avatar of footech
footech
Flag of United States of America image

The output should be Computername, result.
I have no idea what "result" should be.  And what do you mean by capture?  Save to a file?  Multiple files?  Gathering all results together into an array and saving to a variable?

In general I'd recommend something like this.
gc c:\computers.txt | foreach `
{
    Invoke-Command -Computername  $_ -ScriptBlock { Get-ChildItem "c:\appoutput" }
}

Open in new window


or make use of fan-out remoting (running on multiple remote machines at once)
$computers = gc c:\computers.txt
Invoke-Command -Computername  $computers -ScriptBlock { Get-ChildItem "c:\appoutput" }

Open in new window


When you run a command against a remote machine like above, the returned object has a "PsComputerName" property with the value being the name of the computer it was returned from.

You could pipe either of the above commands to a Select statement to choose specific properties and output to a file.  If you wanted to save to a variable you could just preface the Invoke-Command line with "$variable = ".
Avatar of Parity123

ASKER

I want to save the results to a file locally.
If I want to do something for each machine within the script block { if exist c:\appout\123.bin $existentry = "yes", output to local file $computer, $existentry }

Thanks
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks
Thanks