Solved

Powershell : Remoting

Posted on 2016-10-28
6
51 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 40

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 40

Accepted Solution

by:
footech earned 500 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

Industry Leaders: 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!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

740 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