Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How do I PS-Remote to all machines and copy files individually?

$
0
0

Hello,

In the following code, via a Foreach, I create a PS session for a specific machine, then copy files from that specific machine.  How do I create a PS session for all machines (eg. $Sessid = New-Pssession $AllDCs) and copy from all at the same time (eg. Copy-Item -Dest \\ThisBox\C$\Temp\$AllDCs)?  In other words, I dont want to 'single-thread', but want to copy the files from all 3 machines at the same time?  I know PS Remoting can handle it, but I dont know what the logic would be to copy the files in my Scriptblock.

Thanks in advance. 

#
# Housekeeping
$ThisBox    =  $env:ComputerName
$AllDCs     = 'MyDC1','MyDC2','MyDC3'
#
# Foreach DC, create local Dir & copy files from remote server to local dir
foreach ($DC in $AllDCs ) {
$Sessid = New-Pssession $DC 
$Sessid | measure-object  
#
# Create a Directory to park the data
new-item -type directory -path c:\Temp\$DC
# 
# Grab the files
Invoke-Command -session $Sessid -argumentlist $DC,$ThisBox -scriptblock {param($DC,$ThisBox) `
  Get-ChildItem –path 'c:\Program Files (x86)\Data\Logs' -Recurse -Filter *.log  |
  Foreach-Object  { copy-item -Path $_.fullname -Destination "\\$ThisBox\C$\Temp\$DC"	
 }}
#
# Remove the Session IDs
Remove-Pssession $DC
 } # end Foreach


Thanks for your help! SdeDot


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>