1

我有两个循环:

$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

请帮忙。

4

2 回答 2

0

划分之前 WorkingSet64 的值是多少?这会产生一个正数吗?

$WorkingSet = $proc.WorkingSet64/1mb

顺便说一下,当前表达式得到的结果以 KB 为单位(除以 1024)

于 2013-06-18T08:32:28.290 回答
0

您可以使用 .Net ABS 功能:

[Math]::Abs($WorkingSet)
于 2013-06-18T08:52:31.360 回答