Solved

Powershell : Remoting

Posted on 2016-10-28
6
44 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
  • 4
  • 2
6 Comments
 
LVL 39

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 39

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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