CPU 使用脚本

CPU Usage Script

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

$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

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

你有一些错位的波浪线。固定代码如下:

$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
}