title: Trying to get the cpu usage of a specified process on different servers
body: <p>Basically I'm trying to write a script which will retrieve the cpu usage of a process(multiple processes specified in a XML file) from different servers inside a network. </p>
  
  <p>I'm trying to use Invoke-Command to run the script, add the resulting table to collectionVariable and finally convert the result to Html.</p>
  
  <p>Table format</p>
  
  <pre><code>$Format = @"
  &lt;style&gt;
  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;}
  &lt;/style&gt;
  "@
  </code></pre>
  
  <p>Function to parse the xml file with the credentials</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Properties of the item to be selected</p>
  
  <pre><code> $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)}}
   )
  </code></pre>
  
  <p>Inside it I will store the resulting tables for each process</p>
  
  <pre><code>$collectionVariable = New-Object System.Collections.ArrayList
  </code></pre>
  
  <p>Accessing all servers</p>
  
  <pre><code>foreach($j in $server)
   {
   $server = $j   
  </code></pre>
  
  <p>For each server I retrieve data for all the required processes</p>
  
  <pre><code>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) 
    } 
   }
  </code></pre>
  
  <p>Convert the result to Html</p>
  
  <pre><code>ConvertTo-Html -Head $Format -PostContent $collectionVariable |
   Out-File //the out file
  </code></pre>
  
  <p>As a result I should have a html page with tables for each process, but I get the following error. </p>
  
  <pre><code>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             
  </code></pre>