【问题标题】:CPU Usage ScriptCPU 使用脚本
【发布时间】:2016-08-26 16:43:49
【问题描述】:

我也在尝试使用下面提到的脚本来获取内存使用情况(发布在这个link -

$Output = 'C:\temp\Result.txt'
$ServerList = Get-Content 'C:\temp\Serverlist.txt'
$ScriptBLock = {  
$CPUPercent = @{
Label = 'CPUUsed'
Expression = {
$SecsUsed = (New-Timespan -Start $_.StartTime).TotalSeconds
[Math]::Round($_.CPU * 10 / $SecsUsed)

$MemUsage = @{
Label1 ='RAM(MB)' 
Expression = {$_.WS / 1MB}
}
      }
}  
Get-Process | Select-Object -Property Name, CPU, $CPUPercent, $MemUsage,
 Description | 
Sort-Object -Property CPUUsed -Descending | 
Select-Object -First 15  | Format-Table -AutoSize
}
foreach ($ServerNames in $ServerList) {
 Invoke-Command -ScriptBlock $ScriptBLock -ComputerName $ServerNames | 
Out-File $Output -Append
}

我遇到了错误

参数的值为空;需要以下类型之一:{System.String, System.Management.Automation.ScriptBlock}。 + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand + PSComputerName : Server1

你能告诉我我应该更正什么吗?

【问题讨论】:

    标签: powershell cpu-usage invoke-command


    【解决方案1】:

    你有一些错位的曲线。这是固定代码:

    $Output = 'C:\temp\Result.txt'
    $ServerList = Get-Content 'C:\temp\Serverlist.txt'
    $ScriptBLock = {  
      $CPUPercent = @{
        Label = 'CPUUsed'
        Expression = {
          $SecsUsed = (New-Timespan -Start $_.StartTime).TotalSeconds
          [Math]::Round($_.CPU * 10 / $SecsUsed)
        }
      }
      $MemUsage = @{
        Label1 ='RAM(MB)' 
        Expression = {$_.WS / 1MB}
      }
    
    
      Get-Process | Select-Object -Property Name, CPU, $CPUPercent, $MemUsage,
      Description | 
      Sort-Object -Property CPUUsed -Descending | 
      Select-Object -First 15  | Format-Table -AutoSize
    }
    foreach ($ServerNames in $ServerList) {
      Invoke-Command -ScriptBlock $ScriptBLock -ComputerName $ServerNames | 
      Out-File $Output -Append
    }
    

    【讨论】:

    • 仍然收到同样的错误 - Label1 键无效。 + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyIllegal,Microsoft.PowerShell.Commands.SelectObjectCommand + PSComputerName : Server1
    猜你喜欢
    • 2017-01-27
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2013-01-30
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多