Combine PowerShell script into one line

Hi,
i’ve this powershell command the retrieved process CPU usage

(Get-Counter '\Process(*)\% Processor Time').Countersamples | where-object{$_.InstanceName -ne "_total" -and $_.InstanceName -ne "idle" -and  $_.CookedValue/$env:NUMBER_OF_PROCESSORS -gt 5} |Sort CookedValue -Desc | ft  InstanceName,@{Name='CPU %';Expr={[Math]::Round($_.CookedValue / $env:NUMBER_OF_PROCESSORS)}}

and i’ve this powershell command that retrieved process IO usage

Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | where-object{ $_.Name -ne "_Total" -and $_.Name -ne "Idle" -and $_.IODataBytesPersec -gt 1048576 } | Sort-Object IODataBytesPersec -Descending | select  @{Name='Process Name'; E={$_.Name}},@{Name="Total IO Per sec in MB";Expression={[math]::round($_.IODataBytesPersec/1048576,2)}},@{Name="Read IO Per sec in MB";Expression={[math]::round($_.IOReadBytesPersec/1048576,2)}},@{Name="Write IO Per sec in MB";Expression={[math]::round($_.IOWriteBytesPersec/1048576,2)}} | Format-list

how can i combine those 2 powershell scripts into one line powershell script that take the process with the CPU usage and also collect the IOPS usage for it?

i know that Win32_PerfFormattedData_PerfProc_Proces have process CPU usage but it’s not accurate like the CookedValue in Get-Counter ‘\Process(*)% Processor Time’).Countersamples

THX

Avi,

before we proceed, please go back, edit your post and fix the formatting of the code. Without that the forum software tries to “parse” your code and removes some characters like underscores.

To format code as code use the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

i’ve edit the post.

THX

Cool. Thanks so far. :+1:t4:

BTW: It’s considered good style to use line breaks after each pipe symbol. It’ll make your code much easier to read and to understand.
I’d consider adding line breaks after each of your calculated properties as well when you have more than one.
And it’s considered good style as well not to use aliasses. :wink:

Now I’d like to make sure I understood the question correctly.

If you want to run more than one command in sequence you can simply concatenate them with a semicilon or save them in a script and run the script instead of the individual commands.

If you want to combine the results of two commands in one output object they need to have one common property to be combined with.

i can’t add a line breaks because i run this PowerShell from within SQL SERVER and the script must be in one line.

regard my question - what i want and don’t know how to do it is to take the process name and the CPU usage from the first powershell and pass the process name to the second powershell and get the IOPS for the same process in one line\sentence.

Well, without having experiences with SQL server - that sounds weird anyway. Can’t you just provide the path of the script?

Anyway … if I got it right you may tweak the following snippet to match your needs:

(Get-Counter '\Process(*)\% Processor Time').Countersamples |
Where-Object { $_.InstanceName -ne '_total' -and $_.InstanceName -ne 'idle' -and $_.CookedValue / $env:NUMBER_OF_PROCESSORS -gt 5 } |
Sort-Object -Property CookedValue -Descending |
ForEach-Object {
    $Filter = "Name='{0}'" -f $_.InstanceName
    Get-CimInstance -ClassName Win32_PerfFormattedData_PerfProc_Process  -Filter $Filter |
    Select-Object -Property IODataBytesPersec, IOReadBytesPersec, IOWriteBytesPersec
}

thanks.
i put all the script together in on line.
how do i also pass the CPU usage to the final output?

(Get-Counter '\Process(*)\% Processor Time').Countersamples | Where-Object { $_.InstanceName -ne '_total' -and $_.InstanceName -ne 'idle' -and $_.CookedValue / $env:NUMBER_OF_PROCESSORS -gt 1 } | Sort-Object -Property CookedValue -Descending | ForEach-Object {$Filter = "Name='{0}'" -f $_.InstanceName ;Get-CimInstance -ClassName Win32_PerfFormattedData_PerfProc_Process  -Filter $Filter |select  @{Name='Process Name'; E={$_.Name}},@{Name="Total IO Per sec in MB";Expression={[math]::round($_.IODataBytesPersec/1048576,2)}},@{Name="Read IO Per sec in MB";Expression={[math]::round($_.IOReadBytesPersec/1048576,2)}},@{Name="Write IO Per sec in MB";Expression={[math]::round($_.IOWriteBytesPersec/1048576,2)}}}

If you need it in your case for a technical reason it’s fine. But that does not mean you have to do it here as well. Here you’re dealing with human beings willing to help you.

i don’t understand what you means.
what I did wrong here?