locked
PowerShell Multi-threading RRS feed

  • Question

  • Hey all,

    First post here btw so go easy...

    I started using PowerShell to automate a few tasks at the job I work in. Infrastructure and Desktop Support (PowerShell Knoob).

    Just wondering, I have a script, first It scans all IP's in a sub-net with a Foreach loop and returns the DNS host names in a CSV.  I then filter the returned data in the CSV to only display the cells begging with GBD (All our desktops start with this) and save this data on C:\ as computers.txt. Then, I run another script to wait until a time I specify, then run a simple for-each loop or Foreach-Object command to shut down all the target machines in computers.txt.

    As I sometimes have to scan over 10.000 IP's and shutdown at most 3.000 desktops, I was wondering if there was a way to speed up this process through multi threading with workflows, jobs etc. I am new to scripting so try and break things down for me lol.

    Note, the laptop I run the script from has PowerShell 5 however the whole company uses PowerShell 2.0 so will this be possible?

    A colleague mentioned that on my PC I am only writing in PS 5 however my machine cannot run commands in it with multi threading. Is this true?.

    Before you suggest Windows Task Scheduler sadly it's blocked here.

    I am open to any suggestions even if you have alternative ways of doing this. Thanks.

    #Script 1 Sub-net scan
    
    
    
    
    cls
    
    get-date
    $ips = Get-Content C:\temp\ips.txt
    $liveips = $null
    Write-Host "`nPlease wait...`n" -ForegroundColor Cyan
    
    
    foreach ($ip in $ips) {
    
    if (Test-Connection -ErrorAction SilentlyContinue $ip -quiet -count 1 ) { [array]$liveips = $liveips + "$ip"; write $ip }
    
    }
    
    $liveips | ForEach-Object { ([system.net.dns]::GetHostByAddress($_)).hostname | Out-File -Append -Force c:\temp\computers.csv }
    
    
    get-date
    
    

    #Script 2 shutdown
    
    
    cls
    do {
    Start-Sleep 1=
    }
    until ((get-date) -ge (get-date  -Date 07/04/2018 -Hour 00 -Minute 00 ))
    
    write-host "Starting shut down..." -ForegroundColor Cyan
    get-date
    echo ""
    
    
    $computers = Get-Content C:\Shutdown\computers.txt
    
    foreach ( $computer in $computers ) {  
    
    
    stop-Computer -ErrorAction Continue -ComputerName $computer -Force;
     
    }
    
    Get-Date

    • Edited by Abdul-Rehman Karim Monday, April 16, 2018 9:56 AM
    • Moved by Bill_Stewart Wednesday, May 30, 2018 8:56 PM This is not "design solution for me" forum
    Monday, April 16, 2018 9:54 AM

All replies

  • Hi,

    You could use Jobs to restart multiple computers at the same time (parallel).

    Have a look at Start-Job

    Monday, April 16, 2018 10:36 AM
  • Thanks for the reply mate. 

    Could you write me an example. and will this work in parallel on target machines with Powerhell 2?

    Monday, April 16, 2018 11:58 AM
  • You could at the -AsJob parameter to the Stop-Computer command.

    When you specify the AsJob parameter, the command immediately returns an object that represents the background job. You can continue to work in the session while the job finishes. (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/stop-computer?view=powershell-6)

    Monday, April 16, 2018 12:15 PM
  • Sadly powershell remoting is disabled at my company so i cant use that. Any other way? 
    Monday, April 16, 2018 2:24 PM
  • Jobs do not require remoting.

    A workflow would be a better choice.


    \_(ツ)_/

    Monday, April 16, 2018 2:26 PM
  • Will this work if the environment i'm in is locked to PowerShell v2. however I have V5 on my laptop??
    Monday, April 16, 2018 2:42 PM