我有两个循环:
$computer = $args[0]
$colProcData = @()
$procs = get-process -computername $computer | sort -descending "WorkingSet" | select -first 3
foreach($proc in $procs)
{
$machine = $proc.MachineName
$process = [String]$proc.ProcessName
$WorkingSet = [int64]($proc.WorkingSet64/1024)
$id = $proc.Id
$CPU = ($proc.CPU)
$procdata = new-object psobject
$procdata | add-member noteproperty machine $machine
$procdata | add-member noteproperty process $process
$procdata | add-member noteproperty usedRam-MB $WorkingSet
$procdata | add-member noteproperty id $id
$procdata | add-member noteproperty CPU $CPU
$colProcData += $procdata
}
$colProcData | Sort-Object usedRam-MB -descending | Format-Table -AutoSize | out-default
$colProcData = @()
$procs = get-process -computername $computer | sort -descending "WorkingSet" | select -last 3
foreach($proc in $procs)
{
$machine = $proc.MachineName
$process = [String]$proc.ProcessName
$WorkingSet = [int64]($proc.WorkingSet64/1024)
$id = $proc.Id
$CPU = ($proc.CPU)
$procdata = new-object psobject
$procdata | add-member noteproperty machine $machine
$procdata | add-member noteproperty process $process
$procdata | add-member noteproperty usedRam-MB $WorkingSet
$procdata | add-member noteproperty id $id
$procdata | add-member noteproperty CPU $CPU
$colProcData += $procdata
}
$colProcData | Sort-Object usedRam-MB -descending | Format-Table -AutoSize | out-default
唯一的区别是第一个/最后一个。当我运行“.\scriptName.ps1 systemName
”时,我得到:
process usedRam-MB id CPU
------- ---------- -- ---
mysqld 135812 1692
WmiPrvSE 79560 6184
powershell 51724 6644
process usedRam-MB id CPU
------- ---------- -- ---
LogonUI 264 580
Idle 24 0
sqlservr -1987392 1640
如何摆脱减去?仅供参考:systemName是远程计算机,而不是localhost!如果我在localhost上运行它我没有减去:
process usedRam-MB id CPU
------- ---------- -- ---
mysqld 135888 1692
WmiPrvSE 80508 6184
svchost 50720 832
process usedRam-MB id CPU
------- ---------- -- ---
sqlservr 10618744 1640
LogonUI 264 580
Idle 24 0
另有资料:
systemName上的内存总计:12.000 MB
请帮忙。
答案 0 :(得分:0)
在分割之前,WorkingSet64的价值是什么?这会产生正数吗?
$WorkingSet = $proc.WorkingSet64/1mb
顺便说一下,当前表达式得到KB的结果(一次除以1024)
答案 1 :(得分:0)
您可以使用.Net ABS功能:
[Math]::Abs($WorkingSet)