Don't beat me up to much guys, I was trying to understand the other similair questions asked about this issue, just wasn't able to decipher them.
I'm using the below code snipet to copy folders, seperately, on a "remote" computer using Start-Job
:
$Folders = @('Desktop','Documents','Favorites','Links','Downloads','Music','Videos','Pictures','Contacts')
Foreach($Folder in $Folders){
$ArgumentsArray = @()
$ArgumentsArray += "\\$Env:COMPUTERNAME\c$\Users\Abraham\$Folder"
$ArgumentsArray += "C:\CopyBackup"
$RoboCopy_Args = @("/E","/R:5","/W:1","/MT:13")
Start-Job -ScriptBlock { & C:\Windows\System32\Robocopy.exe $args[0] "$($args[1])\$using:Folder" $using:RoboCopy_Args } -ArgumentList $ArgumentsArray -Name $Folder
}
The code works as intended.
Now, the issue is when I try to invoke the command remotely using Invoke-Command -AsJob
. Error reads:
The pipeline was not run because a pipeline is already running. Pipelines cannot be run concurrently.
Which I tried to read and understand before coming here but, wasn't able to understand why it won't work. What's odd, is that the first folder in the $Folders
array (Desktop) does get copied as a job, shows completed, but the other show failed immediately. So I ran Receive-Job -Name Documents
and is where I got the above error.
- What does this have to do with the pipeline?
- Is there a work around solution?
Failed Code:
$PSSession = New-PSSession -ComputerName $env:COMPUTERNAME -ErrorAction Stop
$Folders = @('Desktop','Documents','Favorites','Links','Downloads','Music','Videos','Pictures','Contacts')
Foreach($Folder in $Folders){
$ArgumentsArray = @()
$ArgumentsArray += "C:\Users\Abraham\$Folder"
$ArgumentsArray += "C:\CopyBackup"
$RoboCopy_Args = @("/E","/R:5","/W:1","/MT:13")
Invoke-Command -ScriptBlock { & C:\Windows\System32\Robocopy.exe $args[0] "$($args[1])\$using:Folder" $using:RoboCopy_Args } -AsJob -ArgumentList $ArgumentsArray -Session $PSSession -JobName $Folder
}
Get-Job
output:
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
518 Desktop RemoteJob Running True DESKTOP-OEREJ77 & C:\Windows\System32...
520 Documents RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
522 Favorites RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
524 Links RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
526 Downloads RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
528 Music RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
530 Videos RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
532 Pictures RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
534 Contacts RemoteJob Failed False DESKTOP-OEREJ77 & C:\Windows\System32...
I'd like to stick with running this "as jobs" due to the event produced by it, StateChanged
, which im using for a different purpose.
EDIT:
I've decided to wrap it all into one single Invoke-Command
and send it over, but it's still copying one at a time.
$PSSession = New-PSSession -ComputerName $env:COMPUTERNAME -ErrorAction Stop
Invoke-Command -ScriptBlock {
$Folders = @('Desktop','Documents','Favorites','Links','Downloads','Music','Videos','Pictures','Contacts')
foreach ($Folder in $Folders) {
$ArgumentsArray = @()
$ArgumentsArray += "C:\Users\Abraham\$Folder"
$ArgumentsArray += "C:\CopyBackup"
$RoboCopy_Args = @("/E","/R:5","/W:1","/MT:13")
Start-job {
& C:\Windows\System32\Robocopy.exe $args[0] "$($args[1])\$using:Folder" $using:RoboCopy_Args
} -ArgumentList $ArgumentsArray -Name $Folder
}
} -AsJob -Session $PSSession -JobName $Folder