Thursday, August 16, 2018

powershell script to get some performance stats

#////////////////////////////////////////////////////////////////////////////////////////////


Param($globalSelectedItems)

foreach ($globalSelectedItem in $globalSelectedItems) {

$selectedObject = $globalSelectedItem["DisplayName"]
$hostpath = $globalSelectedItem["Path"]      #Get the server name
}

$class = get-scomclass -Name Microsoft.Windows.Server.OperatingSystem

#Look for OS instance of server
$selectedserverOS = Get-SCOMClassInstance -class $class | ? {$_.Path -ilike $hostpath}

$dataObject = $ScriptContext.CreateInstance("xsd://OSMEMPROC!val/stat")

$dataObject["Id"] = "ItemSelected"
$dataObject["Path"] = $hostpath

$aggregationInterval = 60

#Last 60 minutes (1hr)
  $dt = New-TimeSpan -minute $aggregationInterval   
  $now = Get-Date 
  $from = $now.Subtract($dt)

#Note: the GetMonitoringPerformanceData retrieves data recorded in UTC. So you may need to add some extra lines in the script to handle that as some local times may be a future date and no data will be returned.

$perfRules = $selectedserverOS.GetMonitoringPerformanceData()

foreach ($perfRule in $perfRules)
{
#Get % Processor Time Stat
if($perfRule.CounterName  -eq "% Processor Time")  {
      $data = $perfRule.GetValues($from, $now) | % { $_.SampleValue } | Measure-Object –Average 
 
     $CPUStat =  [math]::round($data.Average,2)
     $dataObject["CPUStat"] = $CPUStat
  }
#Get % Memory Used Stat
  if($perfRule.CounterName  -eq "PercentMemoryUsed")  {
      $data = $perfRule.GetValues($from, $now) | % { $_.SampleValue } | Measure-Object –Average 

     $MemState = [math]::round($data.Average,2)
     $dataObject["MemStat"] =  $MemState
  }
}

$ScriptContext.ReturnCollection.Add($dataObject)

#////////////////////////////////////////////////////////////////////////////////////////////


No comments:

Post a Comment