2

I could use some assistance with adding the parallel processing code to this script. I suppose I just do not understnad powershell enough yet to understand where and what I add the necessary code....I am trying to figure it out but the light bulb is not on just yet...:-)

Start-Transcript C:\temp\agent.log -IncludeInvocationHeader
$computers = gc "C:\temp\list.txt"
$source = "\\pathtodfsrootstore"
$dest = "c$\windows\temp\test2533212"

foreach ($computer in $computers) {
    foreach (if (Test-Connection -Cn $computer -count 1 -quiet) {
        Copy-Item -Force $source -Destination    \\$computer\$dest -Recurse
        #psexec.exe \\$computer cmd /c "c:\windows\temp\testv2533212\test.bat"
    } else {
        Write-output "$computer is not online"
    }
}
Stop-Transcript
  • 1
    Possible duplicate of [Running tasks parallel in powershell](https://stackoverflow.com/questions/43685522/running-tasks-parallel-in-powershell) – henrycarteruk Jun 26 '17 at 16:10
  • Afternoon, maybe a duplicate, but I am hoping someone will throw me a bone with getting me started down the right path here.... – Tony Strother Jun 26 '17 at 16:40

2 Answers2

1

I think you could do something like this:

Start-Transcript C:\temp\agent.log -IncludeInvocationHeader

$Computers = Get-Content "C:\temp\list.txt"
$Source = "\\pathtodfsrootstore"
$Dest = "c$\windows\temp\test2533212"

$TestComputers = Test-Connection -Count 1 -AsJob $Computers
$TestResults = $TestComputers | Wait-Job | Receive-Job

$AliveComputers = ($TestResults | Where {$_.StatusCode -eq 0}).Address

Invoke-Command -ComputerName $AliveComputers -ScriptBlock {
    Copy-Item -Force $source -Destination c:\windows\temp\test2533212 -Recurse 
    & "c:\windows\temp\testv2533212\test.bat"
}

Stop-Transcript

This is a two staged approach, where we use the -AsJob switch of Test-Connection to do a parallel test of all the computers to find out which are alive, then use the results of this to do the other job in parallel.

The paths have been changed in the jobs as they will be running on the remote machines, so can reference local paths.

I haven't tested this, so it may need some tweaking. You could also just disregard the Test-Connection part at the beginning and just allow the jobs to fail where the machines aren't reachable.

With the above solution if you wanted to know which machines weren't reachable you could do this:

$DeadComputers = ($TestResults | Where {$_.StatusCode -ne 0}).Address
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
0

For parallel processing ,u might wanna explore on powershell workflow, it had foreach-parellel component , which will handle parallel processing like a charm

Workflow basics

A snippet of parallel is as follows

workflow Start-Something {

      foreach -Parallel($i in 0..1000)
      {
      $i
      }

      }

Start-Something

In the above example for-each runs in parallel,There is a limitation in powershell workflow ,That is,We cant have Write-Output,You might wanna keep a note of it,when u try to solve your problem

Just trying to make changes on top of your code,You may wanna spend some 15 mins,n your solution in particular,But work on these lines

#Creating Workflow here ,
workflow Start-Something {

Start-Transcript C:\temp\agent.log -IncludeInvocationHeader
$computers = gc "C:\temp\list.txt"
$source = "\\pathtodfsrootstore"
$dest = "c$\windows\temp\test2533212"

foreach -Parallel($computer in $computers) {
if (Test-Connection -Cn $computer -count 1 -quiet) {
        Copy-Item -Force $source -Destination    \\$computer\$dest -Recurse
        #psexec.exe \\$computer cmd /c "c:\windows\temp\testv2533212\test.bat"
    } else {
       #You should log it somewhere
    }
}
Stop-Transcript
      }  


#Calling Workflow
Start-Something
Chetan Kulkarni
  • 404
  • 4
  • 15