Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add multi-threading?

Is there a way of getting the below to run in parallel (multi-threading)? I have about 200 servers that need to run and was wondering if there is a way of checking say 10 servers at once rather then one at a time...WMI is very slow in checking this one 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***"
    }
}
like image 208
lara400 Avatar asked May 03 '13 13:05

lara400


People also ask

How is Multi-threading achieved?

Multithreading in Python can be achieved by importing the threading module. Now that you have threading module installed, let us move ahead and do Multithreading in Python.

How do I enable multiple threads in Python?

To use multithreading, we need to import the threading module in Python Program. A start() method is used to initiate the activity of a thread. And it calls only once for each thread so that the execution of the thread can begin.

Who can do multi-threading?

Processor based multitasking is totally managed by the OS, however multitasking through multithreading can be controlled by the programmer to some extent. The concept of multi-threading needs proper understanding of these two terms – a process and a thread.


1 Answers

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
like image 120
jbockle Avatar answered Oct 05 '22 00:10

jbockle