Author Posts

April 8, 2017 at 1:20 pm

I wrote a simple script that scans over 200 machines to check the status of a couple of services. It stores the returned data in an array and at the end creates a csv and sens it to me. Trouble is the script runs for over 30 mins as it connects to each machine one by one. How would I make it to scan all the machines at once and then sort the data by hostname?

April 8, 2017 at 3:24 pm

Edit: Can you post your code? Please do so between pre /pre tags.

April 8, 2017 at 3:25 pm

Will you post your script? You may be running in serial rather than parallel. PowerShell Remoting must be enabled.

# Establish 32 concurrent connections at once (Parallel)
Invoke-Command -ComputerName (Get-Content serverlist.txt) {
     Get-Service yourservice   
}

# Execute for one computer then the next (Serial)
foreach ($c in (Get-Content serverlist.txt)) {
    Invoke-Command -ComputerName $c {Get-Service yourservice}}

April 8, 2017 at 6:45 pm

I use the serial method

foreach($item in $comps){
   $status = invoke-command $item -scriptblock {get-service | where{$_.name -eq "my_service} | select -expandproperty status}
}
$x = @{
    cn = $item
    status = $status
}
$report += New-Object psobject -property $x
$report | select cn,status | export-csv $csv_file

April 9, 2017 at 1:50 am

You can use Invoke-Parallel script to 'multithread' your script using runs spaces..

April 9, 2017 at 12:16 pm

Try something like this.

$comps = @('comp1','comp2','comp3')
Invoke-Command -ComputerName $comps {Get-Service myservice} | 
Select-Object Status,Name,DisplayName,PSComputerName | 
Export-Csv .\file.csv -NoTypeInformation

April 26, 2017 at 8:36 am

For a test I tried

$feature = Invoke-Command -ComputerName $comps {$env:computername; (Get-WindowsFeature feature1).installed}
$feature

Problem is when I get the results every now and then I'll get a series of computernames followed by series of booleans rather than computername followed by the answer so it's difficult to tell whether the boolean are in the same order like the computernames e.g.

computer1
true 
computer2
computer3
computer4
true
false 
true