How to add multithreading?

Is there a way to get parallel execution of parallel work (multithreading)? I have about 200 servers that need to be started, and I was wondering if there is a way to check, for example, 10 servers at the same time, and not one at a time ... WMI very slowly checks it at a time.

clear Write-Host "Script to Check if Server is Alive and Simple WMI Check" $servers = Get-Content -Path c:\Temp\Servers.txt foreach($server in $servers) { if (Test-Connection -ComputerName $server -Quiet) { $wmi = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $server).Name Write-Host "$server responds: WMI reports the name is: $wmi" } else { Write-Host "***$server ERROR - Not responding***" } } 
+6
source share
1 answer

Use powershell jobs:

 $scriptblock = { Param($server) IF (Test-Connection $server -Quiet){ $wmi = (gwmi win32_computersystem -ComputerName $server).Name Write-Host "***$server responds: WMI reports the name is: $wmi" } ELSE { Write-Host "***$server ERROR -Not responding***" } } $servers | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null} Get-Job | Wait-Job | Receive-Job 
+8
source

Source: https://habr.com/ru/post/944216/


All Articles