Author Posts

August 25, 2015 at 12:01 am

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

August 25, 2015 at 12:04 am

Hello,

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

/Alexander

August 25, 2015 at 1:00 am

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

August 25, 2015 at 3:31 am

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.

August 25, 2015 at 8:40 am

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

workflow Get-ProcessInfo
        {
            #Run commands in parallel.
            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.