Solved

Powershell : Remoting

Posted on 2016-10-28
6
31 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
Comment Utility
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
Comment Utility
I want to save the results to a file locally.
0
 

Author Comment

by:Parity123
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
$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
Comment Utility
Thanks
0
 

Author Closing Comment

by:Parity123
Comment Utility
Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now