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
    $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 { ([]::GetHostByAddress($_)).hostname | Out-File -Append -Force c:\temp\computers.csv }

    #Script 2 shutdown
    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
    echo ""
    $computers = Get-Content C:\Shutdown\computers.txt
    foreach ( $computer in $computers ) {  
    stop-Computer -ErrorAction Continue -ComputerName $computer -Force;

    • 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