sequences - slow script

This topic contains 6 replies, has 4 voices, and was last updated by  JC 7 months, 3 weeks ago.

  • Author
    Posts
  • #68343

    JC
    Participant

    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

    Aaron Hardy
    Participant

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

  • #68347

    random commandline
    Participant

    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

    JC
    Participant

    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

    Sam Boutros
    Participant

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

  • #68373

    random commandline
    Participant

    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

    JC
    Participant

    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

You must be logged in to reply to this topic.