powershell可以像任务管理器一样返回值



我想使用powershell来返回值,喜欢任务管理器
但是,网络使用项目无法正确返回
如何修改代码?非常感谢。

$RAM= Get-WMIObject Win32_PhysicalMemory | Measure -Property capacity -Sum | %{$_.sum/1Mb}
$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) {
$tmp = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | 
select-object -property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime/$cores)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}}, @{"Name" = "Memory(%)"; Expression = {([math]::Round(($_.WorkingSetPrivate/1Mb)/$RAM*100,2))}}, @{Name="Disk(MB)"; Expression = {[Math]::Round(($_.IODataOperationsPersec / 1mb),2)}}, @{"Name"="Network"; Expression = {get-counter "Process($_.Name)IO Read Bytes/sec"}} |
Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} |
Sort-Object -Property CPU -Descending|
Select-Object -First 5;
cls
$tmp | Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)", "Memory(%)", "Disk(MB)", "Network";
Start-Sleep 3
}

此代码是根据以下文章修改的:
1.Powershell中所有进程的CPU和内存使用率(百分比(
2。https://superuser.com/questions/1314534/windows-powershell-displaying-cpu-percentage-and-memory-usage#new-答案?newreg=a9290334d72946db9c7f8fd2af10de0a
3。Powershell脚本列表进程与cpu,内存,磁盘使用


我添加了一个变量$process来显示每个进程的所有者,但缺少一些参数。我找不到任何关于从microsoft获取WmiObject Win32_PerfFormattedData_PerfProc_Process的文档,这正常吗?修改后的代码如下:

$RAM= Get-WMIObject Win32_PhysicalMemory | Measure -Property capacity -Sum | %{$_.sum/1Mb}
$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
$Process = Get-Wmiobject Win32_process -computername "myComputerName" | select *,@{Name='Owner';Expression={($_.GetOwner()).User}}
while ($true) {
$tmp = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | 
select-object -property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime/$cores)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}}, @{"Name" = "Memory(%)"; Expression = {([math]::Round(($_.WorkingSetPrivate/1Mb)/$RAM*100,2))}}, @{Name="Disk(MB)"; Expression = {[Math]::Round(($_.IODataOperationsPersec / 1mb),2)}}, @{"Name"="Network"; Expression = { $_.IOReadBytesPersec }}, @{Name="Username"; Expression = {($($Process | ?{$_.ProcessId -eq $Item.IDProcess})).Owner} |
Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} |
Sort-Object -Property CPU -Descending|
Select-Object -First 15;
cls
$tmp | Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)", "Memory(%)", "Disk(MB)", "Network", "Username";
Start-Sleep 1
}

修改后的零件参照自https://powershell.org/forums/topic/getting-percentage-cpu-time-and-owner-for-processes/

郭,欢迎光临SO.

首先,您的"网络"部分没有显示任何内容的原因是因为变量扩展失败。让我解释一下——虽然PowerShell理解在双引号字符串内部展开的基本变量,但它并不直接理解具有自己属性的稍微复杂一些的对象。所以你的问题就在这里:

@{"Name"="Network"; Expression = {get-counter "Process($_.Name)IO Read Bytes/sec"}}

由于'$_.Name'位于双引号内,PowerShell不知道该如何处理它

@{"Name"="Network"; Expression = {get-counter "Process($($_.Name))IO Read Bytes/sec"}}

或使用字符串令牌:

@{"Name"="Network"; Expression = {get-counter ("Process({0})IO Read Bytes/sec" -f $_.Name)}}

然而,在我要复制的测试中,只需运行GetCounter就会返回一个复杂的对象。也就是说,一个对象有自己的属性和方法,所以这无论如何都不应该起作用。在确定结果属性的目标时,我认为你会追求(Get-Counter"\Process(ProcessName(\IO Read Bytes/sec"(.CounterSamples.CookedValue,脚本根本没有返回数据——就像Get-Counters花费的时间太长或其他什么。

但是,"Win32_PerfFormattedData_PerfProc_Process"似乎已经为您想要数据的计数器具有等效属性:IOReadBytesPersec。

我唯一要注意的是,该属性和"IO读取字节/秒"的进程特定性能计数器都计算所有I/O,而不仅仅是网络。事实上,在查看处理器对象时,我找不到任何只针对Network的计数器。

当我运行它时,你的CPU什么都不显示,因为在很多情况下你试图除以0。查看PercentProcessorTime,它没有指定任何关于所有处理器总数的信息。我不会像你的代码那样假设负载是在内核之间分配的——我可能错了。

最后,从性能的角度来看,您可以通过重新安排您正在做的一些事情来显著减少脚本对硬件的影响:

  1. 从"Win32_PerfFormattedData_PerfProc_Process"显式请求所需属性,这样它就不会默认为所有属性
  2. 将"选择对象-第一个5"与第一个"选择对象"合并,以便在循环的其余部分只处理这5个对象

下面是一些稍微修改过的代码,显示了我所描述的一些更改:

$RAM= Get-WMIObject Win32_PhysicalMemory | Measure -Property capacity -Sum | %{$_.sum/1Mb}
$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) {
$tmp = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | 
select-object -First 5 -Property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}}, @{"Name" = "Memory(%)"; Expression = {([math]::Round(($_.WorkingSetPrivate/1Mb)/$RAM*100,2))}}, @{Name="Disk(MB)"; Expression = {[Math]::Round(($_.IODataOperationsPersec / 1mb),2)}}, @{"Name"="Network"; Expression = { $_.IOReadBytesPersec }} |
Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} |
Sort-Object -Property CPU -Descending
cls
$tmp | Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)", "Memory(%)", "Disk(MB)", "Network";
Start-Sleep 3
}

最新更新