Run 2 cmdlets in parallel

This topic contains 4 replies, has 4 voices, and was last updated by  Curtis Smith 2 years, 9 months ago.

  • Author
  • #28939


    is it possible? i am trying to get cpu usage of server as well as top process consuming more cpu, looks like these 2 cannot be retrieved in a same cmdlet

  • #28940

    Alexander Johansson


    Considering the lack of information of what you're trying to achieve, would the "Job-cmdlets" help you? Like Start-Job etc?


  • #28941


    i am just creating a table with cpu % and process name which consumes more % ,

    For cpu % – i am using
    Get-WmiObject win32_processor |Measure-Object -property LoadPercentage -Average | Select Average

    For getting the process name and % it consumes cpu, i am using,
    Get-WmiObject Win32_PerfFormattedData_PerfProc_Process

    But these 2 are running one after another, that means the data wont be the same the moment i run the first line it gives me cpu% and the next line gives me the name and % of the process which is utilising more cpu.

    Lets say First line gives me 25% and the next line gives me sqlservr 26% , which is of no use as these 2 will be contradicting. Thats the reason i want to run these 2 cmdlets in parallel and get a table out of it

  • #28946

    Dave Wyatt

    That'll be a bit awkward in PowerShell, as you'll need at least three threads going. (Two to collect data, one to display the table as it comes in.) It can be done, but it's not as easy as in something like C#, which has constructs to make parallel processing easier.

  • #28959

    Curtis Smith

    To run two commands at the same time, you can use a WorkFlow. Something like this.

    workflow Get-ProcessInfo
                #Run commands in parallel.
                    Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select @{Name="Name";Expression={"Average"}}, @{Name="PercentUserTime";Expression={$_.Average}}
                    Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | Select Name, PercentUserTime, PercentPrivilegedTime, PercentProcessorTime
    Get-ProcessInfo | ft

    Another option would be to use your `"Get-WmiObject Win32_PerfFormattedData_PerfProc_Process"` command and then sum the relevant column to get the total process time.

You must be logged in to reply to this topic.