0

I am also trying to get memory usage using the script mentioned below (Posted in this 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
}

I am getting error

The value of a parameter was null; one of the following types was expected: {System.String, System.Management.Automation.ScriptBlock}. + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand + PSComputerName : Server1

Can you pls let me know what should I correct ?

Community
  • 1
  • 1
V T
  • 47
  • 10

1 Answers1

1

You had some misplaced squiggelies. Heres the fixed code:

$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
}
restless1987
  • 1,558
  • 10
  • 16
  • Still getting the same error - The Label1 key is not valid. + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyIllegal,Microsoft.PowerShell.Commands.SelectObjectCommand + PSComputerName : Server1 – V T Aug 26 '16 at 17:17