sequences - slow script

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of random commandline random commandline 2 weeks, 2 days ago.

  • Author
    Posts
  • #68343
    Profile photo of JC
    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
    Profile photo of Aaron Hardy
    Aaron Hardy
    Participant

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

  • #68347
    Profile photo of random commandline
    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
    Profile photo of JC
    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
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

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

  • #68373
    Profile photo of random commandline
    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
    

You must be logged in to reply to this topic.