locked
How to use Invoke-Parallel or another method to use multiple threads RRS feed

  • Question

  • Hi 

    I am quite new to scripting and could use a little help getting Invoke-parallel to work.

    I have an array of computer objects with certain properties like Name, IP, Ping and RPC. This is called  $Computers.

    I need to check each of those objects for running explorer processes and get the owner(username) of those processes.

    The below works but takes forever with several hundreds of Computer objects.

    $Username = Read-Host "Enter username you want to search for"
    foreach ($Comp in $Computers)
     {
     IF($Comp.Ping -eq $true -and $Comp.RPC -eq $true) 
          {  #Get explorer.exe processes
      $proc = gwmi win32_process -computer $Comp.Name -Filter "Name = 'explorer.exe'" -ErrorAction 0
            #Search collection of processes for username
      ForEach ($p in $proc) {
          $temp = ($p.GetOwner()).User
                $temp
              if ($temp -eq $Username){
       write-host "$Username is logged on to:" $Comp.Name "IP:" $Comp.IP
       }}}}

    I tried several ways to make the "$proc = gwmi win32_process -computer $Comp.Name -Filter "Name = 'explorer.exe'" -ErrorAction 0" run in parallel, but i dont succeed.

    I tried using foreach-parallel.ps1 and invoke-parallel.ps1

    Example of current syntax which is not working

    ### example of foreach-parallel ###

    $Computers | ForEach-Parallel -ScriptBlock { IF($_.Ping -eq $true -and $_.RPC -eq $true) { #Get explorer.exe processes $proc = gwmi win32_process -Filter "Name = 'explorer.exe'" -ErrorAction 0 #Search collection of processes for username ForEach ($p in $proc) { $temp = ($p.GetOwner()).User if ($temp -eq $Username){ Write-Host "$Username is logged on to:" $_.Name "IP:" $_.IP }}}}

    ### example of Invoke-parallel ###

    Invoke-Parallel -InputObject $OnlineComputers -Throttle 20 -runspaceTimeout 30 -ScriptBlock {
     
          IF($_.Ping -eq $true -and $_.RPC -eq $true) 
          {  #Get explorer.exe processes
      $proc = gwmi win32_process -computer $_.Name -Filter "Name = 'explorer.exe'" -ErrorAction 0
            #Search collection of processes for username
      ForEach ($p in $proc) {
          $temp = ($p.GetOwner()).User
                if ($temp -eq $Username){
       Write-Host "$Username is logged on to:" $_.Name "IP:" $_.IP
       }}}
    }


    Anyone who has an idea of what i am doing wrong or an idea of another way to achieve the goal ?





    Friday, March 9, 2018 10:30 AM

Answers

  • Invoke-Parallel -InputObject $Comp.name -ImportVariables -Throttle 32
    -runspaceTimeout 30 -NoCloseOnTimeout -ErrorAction SilentlyContinue -ScriptBlock {

    #Get explorer.exe processes where Owner equals Username $proc = gwmi win32_process -computer $_ -Filter "Name = 'explorer.exe'" $TempProc = $proc.GetOwner().User if ($TempProc -eq $username){ Write-host $Username "is logged on to:" $_ } }


    • Marked as answer by Martin_Nielsen Saturday, March 10, 2018 12:27 PM
    Saturday, March 10, 2018 10:46 AM