2

我喜欢 PSList,并使用 CPU 和 Elapsed 时间来识别需要杀死的进程。我想将它包装在一个返回管道值的 powershell 函数中,以便我可以执行以下操作:

get-remoteprocess server1 | where  {$_.CPUMinutes -gt 4}

我被困在从函数返回值中——我知道这需要是一个对象数组。

这是我的第一个版本的样子:

function get-remoteproc {
param ($hostname)
$results = pslist "\\$hostname" 

for ($i= 3; $i -le $results.length; $i++) {
    $strline = $results[$i]
    $StrWithPrefix = " "+$results[$i]
    $trimmedline = $StrWithPrefix -replace '\s+', " ";
    $Splitline = $trimmedline.Split(" ")
    $ProcessName = $Splitline[1]
    .
    .
    $ProcessCPUTime = ":"+[string]$Splitline[7]
    $SplitCpuTime = $ProcessCPUTime.Split(":")

    $CpuHours = $SplitCpuTime[1]
    $CpuMinutes = $SplitCpuTime[2]
    $CpuSeconds = $SplitCpuTime[3]
    .
    .
    .

    $obj = New-Object PSObject
       $obj | Add-Member Noteproperty -Name "Name" -Value $Name
       $obj | Add-Member Noteproperty -Name "PPid" -Value $Ppid
       $obj | Add-Member Noteproperty -Name "Pri" -Value $Pri

}

接受 Doug Finke 的建议,这是令人愉悦的简洁,这是我稍微改编的版本,它适用于管道值。

function get-remoteproc {
    param ($hostname=$env:COMPUTERNAME)

    $results = PsList "\\$hostname"

    foreach($record in $results[3..($results.Count)]) {
        # Remove spaces
        while ($record.IndexOf("  ") -ge 0) {
            $record = $record -replace "  "," "
        }

        $Name,$Processid,$Pri,$Thd,$Hnd,$Priv,$CPUTime,$ElapsedTime = $record.Split(" ")

        $properties = @{
            Name = $Name
            Pid = $Processid
            Pri = $Pri
            Thd = $Thd
            Hnd = $Hnd
            Priv = $Priv
            CPUTime = $CPUTime
            ElapsedTime = $ElapsedTime
        }

        New-Object PSObject -Property $properties |
            Add-Member -PassThru ScriptProperty CPUH   {[int]$this.CPUTime.Split(":")[0]} |
            Add-Member -PassThru ScriptProperty CPUM {[int]$this.CPUTime.Split(":")[1]} |
            Add-Member -PassThru ScriptProperty CPUS {[int]$this.CPUTime.Split(":")[2]} |
            Add-Member -PassThru ScriptProperty ElaH {[int]$this.ElapsedTime.Split(":")[0]} |
            Add-Member -PassThru ScriptProperty ElaM {[int]$this.ElapsedTime.Split(":")[1]} |
            Add-Member -PassThru ScriptProperty ElaS {[int]$this.ElapsedTime.Split(":")[2]} 
    }
}

以及对该函数的调用,这表明对象已正确展开以供管道使用:

get-remoteproc "Server1" | where {(($_.CPUM * $_.CPUS) -gt 60) -and ($_.name -notmatch "Idle" )}|
ft name, pid, pri, thd, hnd, priv, CPUH, cpuM, cpuS, ElaH, ElaM, ElaS   -auto

感谢大家!

4

2 回答 2

1

正如 empo 指出的那样,您需要将 $obj 发送回管道。这是将 plist 文本转换为 PowerShell 对象的另一种方法。

function get-remoteproc {
    param ($hostname=$env:COMPUTERNAME)

    $results = PsList "\\$hostname"

    foreach($record in $results[3..($results.Count)]) {
        # Remove spaces
        while ($record.IndexOf("  ") -ge 0) {
            $record = $record -replace "  "," "
        }

        $Name,$Processid,$Pri,$Thd,$Hnd,$Priv,$CPUTime,$ElapsedTime = $record.Split(" ")

        $properties = @{
            Name = $Name
            Pid = $Processid
            Pri = $Pri
            Thd = $Thd
            Hnd = $Hnd
            Priv = $Priv
            CPUTime = $CPUTime
            ElapsedTime = $ElapsedTime
        }

        New-Object PSObject -Property $properties |
            Add-Member -PassThru ScriptProperty CPUHours   {$this.CPUTime.Split(":")[0]} |
            Add-Member -PassThru ScriptProperty CPUMinutes {$this.CPUTime.Split(":")[1]} |
            Add-Member -PassThru ScriptProperty CPUSeconds {$this.CPUTime.Split(":")[2]} 

    }
}
于 2011-05-08T12:32:09.227 回答
0

要一个一个返回传递给管道的值数组,您只需要在函数中返回多个值。例如:

函数 get-remoteproc {
参数($主机名)
  $results = pslist "\\$主机名"
  for ($i= 3; $i -le $results.length; $i++) {

    # 你的员工到 $obj

    # 输出 $obj
    $obj
  }
}

函数结果是一个数组$obj。当连接到管道时,数组将被注册,所有值将被一一传递给流。

于 2011-05-08T12:16:30.540 回答