Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell : Remoting

Posted on 2016-10-28
6
Medium Priority
?
63 Views
Last Modified: 2016-10-31
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,
0
Comment
Question by:Parity123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 41

Expert Comment

by:footech
ID: 41864473
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 = ".
0
 

Author Comment

by:Parity123
ID: 41864548
I want to save the results to a file locally.
0
 

Author Comment

by:Parity123
ID: 41864565
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Accepted Solution

by:
footech earned 2000 total points
ID: 41864666
$scriptblock = {
    $existEntry = "no"
    If ( Test-Path "c:\appout\123.bin" )
    { $existEntry = "yes" }
    New-Object PsObject -property @{ ExistEntry = $existEntry }
}

$computers = Get-Content c:\computers.txt
Invoke-Command -Computername $computers -ScriptBlock $scriptblock | Select PsComputerName,ExistEntry | Export-CSV c:\temp\filecheck.csv -notype

Open in new window

0
 

Author Comment

by:Parity123
ID: 41867411
Thanks
0
 

Author Closing Comment

by:Parity123
ID: 41867437
Thanks
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question