sequences - slow script

Welcome Forums General PowerShell Q&A sequences - slow script

This topic contains 6 replies, has 4 voices, and was last updated by

YCE
 
Participant
1 year, 7 months ago.

  • Author
    Posts
  • #68343
    YCE

    Participant
    Points: 12
    Rank: Member

    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?

  • #68344

    Participant
    Points: 0
    Rank: Member

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

  • #68347

    Participant
    Points: 10
    Rank: Member

    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}}
    
  • #68355
    YCE

    Participant
    Points: 12
    Rank: Member

    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
  • #68365

    Participant
    Points: 85
    Rank: Member

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

  • #68373

    Participant
    Points: 10
    Rank: Member

    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
    
  • #69534
    YCE

    Participant
    Points: 12
    Rank: Member

    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

The topic ‘sequences - slow script’ is closed to new replies.