尝试获取不同服务器上指定进程的cpu使用情况

时间:2019-05-29 13:33:05

标签: powershell powershell-remoting

基本上,我正在尝试编写一个脚本,该脚本将从网络内部的不同服务器检索进程(在XML文件中指定的多个进程)的cpu使用率。

我试图使用Invoke-Command运行脚本,将结果表添加到collectionVariable,最后将结果转换为HTML。

表格格式

$Format = @"
<style>
TABLE {width:90%;margin-left:5%; border-width: 1px; border-style: solid; 
border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: 
black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: 
black;}
</style>
"@

使用凭证解析xml文件的功能

function parseXmlCredential {
[xml]$xml1 = get-content xmlCredentials\*.xml

$servers = $xml1.Configuration.servers;
$names = $xml1.Configuration.name;
$username = $xml1.Configuration.username;
$password = $xml1.Configuration.password;

$server = $servers -split ','
$name = $names -split ','

return $server, $name, $username, $password
}

$server, $name, $username, $password = parseXmlCredential;

要选择的物品的属性

 $properties = @(
 @{Name = 'Server';  Expression = { $server}},
 @{Name = "Process Name";Expression ={$_.Name}},
 @{Name = "Process Id";Expression = {$_.Id}},
 @{Name = 'CPUPercent(%)';Expression = {
      $TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
      [Math]::Round( ($_.CPU * 100 / $TotalSec), 2)}},
 @{Name ="Memory (MB)";Expression = {[Math]::Round(($_.privatememorysize / 1mb),2)}}
 )

在其中,我将存储每个进程的结果表

$collectionVariable = New-Object System.Collections.ArrayList

访问所有服务器

foreach($j in $server)
 {
 $server = $j   

对于每台服务器,我检索所有必需进程的数据

foreach($i in $name)
 {
 $rezultat = Invoke-Command -ComputerName $j -ScriptBlock {

 $table = Get-Process | 
 Where-Object {$_.Name -like $i+'*' } |
 Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} |
 Select-Object  $properties |
 Sort-Object -Property *CPUPercent* -Descending |
 ConvertTo-Html -As TABLE -Fragment |
 Out-String

 $collectionVariable.Add($table) | Out-Nul
   } -ArgumentList $i,(,$properties),(,$collectionVariable) 
  } 
 }

将结果转换为HTML

ConvertTo-Html -Head $Format -PostContent $collectionVariable |
 Out-File //the out file

结果,我应该有一个带有每个进程表的html页面,但是出现以下错误。

You cannot call a method on a null-valued expression.
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName        : osvm2309

You cannot call a method on a null-valued expression.
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName        : osvm2309

You cannot call a method on a null-valued expression.
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName        : osvm2256

You cannot call a method on a null-valued expression.
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName        : osvm2256

ConvertTo-Html : Cannot validate argument on parameter 'PostContent'. The 
argument is null, empty, or an element of the argument collection 
contains a null value. Supply a collection that does not contain any null 
values and then try the command again.
At C:\Users\lucianp\Downloads\processes_details_powershell\script.ps1:72 
char:43
+ ConvertTo-Html -Head $Header -PostContent $collectionVariable |
+                                           ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [ConvertTo-Html], 
ParameterBindingValidationException    +FullyQualifiedErrorIdParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertToHtmlCommand             

1 个答案:

答案 0 :(得分:0)

我找到了答案: 问题出在collectionVariable上,我试图将结果添加到Invoke-Command内,而应该在$ invoke-command之外添加$ rezultat,然后再添加。