gpt4 book ai didi

powershell - 将过程信息转换为人类可读的值

转载 作者:行者123 更新时间:2023-12-03 01:05:07 26 4
gpt4 key购买 nike

我一直在使用一个函数将字节转换为目录的“友好”大小,如下所示:

脚本:

function Get-FriendlySize {
param($Bytes)
$sizes='Bytes,KB,MB,GB,TB,PB,EB,ZB' -split ','
for($i=0; ($Bytes -ge 1kb) -and
($i -lt $sizes.Count); $i++) {$Bytes/=1kb}
$N=2; if($i -eq 0) {$N=0}
"{0:N$($N)} {1}" -f $Bytes, $sizes[$i]
}

dir 'C:\Windows\ccmsetup' | Select-Object -Property Name, LastWriteTime, @{N='FriendlySize';E={Get-FriendlySize -Bytes $_.Length}}

输出:
Name                                     LastWriteTime         FriendlySize
---- ------------- ------------
cache 7/5/2017 10:47:32 AM 1 Bytes
Logs 5/17/2018 5:18:28 PM 1 Bytes
{6EF23B21-22B7-453C-A975-52723EE71ABA} 1/7/2018 1:06:03 PM 1 Bytes
ccmsetup.cab 1/7/2018 1:04:21 PM 9.42 KB
ccmsetup.exe 11/7/2017 8:00:00 PM 3.55 MB
ccmsetup.xml 12/1/2017 3:55:00 PM 10.51 KB
CLP1031.MSP 11/9/2017 9:00:00 AM 1.77 MB
CLP1036.MSP 11/9/2017 9:00:00 AM 1.77 MB
CLP1041.MSP 11/9/2017 9:00:00 AM 1.77 MB
CLP2052.MSP 11/9/2017 9:00:00 AM 1.77 MB
configmgr1702-client-kb4019926-x64.msp 4/11/2017 11:50:00 AM 7.36 MB
MicrosoftPolicyPlatformSetup.msi 2/28/2017 9:11:22 AM 2.45 MB
SCEPInstall.exe 6/20/2016 12:54:16 PM 27.05 MB
Silverlight.exe 4/10/2017 6:34:48 PM 6.70 MB
WindowsFirewallConfigurationProvider.msi 2/28/2017 9:11:22 AM 589.00 KB

我现在正尝试将相同的逻辑应用于Get-Process输出,以便内存工作集显示为友好而不是默认字节显示,但输出始终为“1字节”。

脚本:
function Get-FriendlySize {
param($Bytes)
$sizes='Bytes,KB,MB,GB,TB,PB,EB,ZB' -split ','
for($i=0; ($Bytes -ge 1kb) -and
($i -lt $sizes.Count); $i++) {$Bytes/=1kb}
$N=2; if($i -eq 0) {$N=0}
"{0:N$($N)} {1}" -f $Bytes, $sizes[$i]
}

Get-Process -Name explorer | Select-Object -Property Handles, @{N='RAM Working Set';E={Get-FriendlySize -Bytes $_.Length}}, CPU, Id, ProcessName

输出:
Handles         : 2521
RAM Working Set : 1 Bytes
CPU : 169.125
Id : 10080
ProcessName : explorer

我究竟做错了什么?

最佳答案

您需要使用$_.WorkingSet而不是$_.Length来获取内存

Get-Process -Name explorer | Select-Object -Property Handles, @{N='RAM Working Set';E={Get-FriendlySize -Bytes $_.WorkingSet}}, CPU, Id, ProcessName

关于powershell - 将过程信息转换为人类可读的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50535131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com