#OS version for Win 2012
$WIN_OS_2012_Ver = "6.2"
$OSRegistryKey = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
#******************************************************************************
# FUNCTION: CheckMinOSVer
# DESCRIPTION: Returns True if the Registry Key for CurrentVersion
# is equal or Higher than the Minimum OS Versions Number.
# PARAMETER: DblMinVer Minimum Version Number to use
# RETURNS: Boolean: True, if build is greater or equal than the given number
#******************************************************************************
function CheckByOSCurrentVersion() #As Boolean
{
$strCurrentOSVer = Get-ItemProperty $OSRegistryKey
$strCurrentOSVer = $strCurrentOSVer.CurrentVersion
if($strCurrentOSVer -ge $WIN_OS_2012_Ver)
{
return $true;
}
return $false;
}
Function Load-CimModules
{
$error.Clear()
$CimModule = Get-Module CimCmdlets
if ($null -eq $CimModule)
{
Import-Module CimCmdlets
$error.Clear()
}
}
function GetProcessorTime ($procId, $ComputerName)
{
# Get the total processor time count ProcessIterationCount number of times, to get the average
for($loopCounter=0; $loopCounter -lt $ProcessIterationCount; $loopCounter++)
{
$agentProcIDs = "|"
# Step 1: Get all SCOM Processes
for($counter=0; $counter -lt $retryAttempts; $counter++)
{
if($isHigherThanWin08 -eq $true)
{
Load-CimModules
try
{
$cimSessionOption = New-CimSessionOption -Protocol DCOM
$cimsession = New-CimSession -ComputerName $ComputerName -SessionOption $cimSessionOption
$processes = Get-CimInstance -CimSession $cimsession -Namespace "root\cimv2" -Query "SELECT ProcessId, ParentProcessId, Name FROM Win32_Process" -ErrorAction Stop
}
catch
{
$processes = Get-WMIObject -Namespace "root\cimv2" -ComputerName $ComputerName -Query "SELECT ProcessId, ParentProcessId, Name FROM Win32_Process" -ErrorAction stop
}
Finally
{
Get-CimSession | Remove-CimSession
$cimsession =$null
$cimSessionOption = $null
}
}
else
{
$processes = Get-WMIObject -Namespace "root\cimv2" -ComputerName $ComputerName -Query "SELECT ProcessId, ParentProcessId, Name FROM Win32_Process" -ErrorAction stop
}
if($processes -ne $null)
{
# Step 2: Get the Health Service and Monitoring Host objects
foreach($process in $processes)
{
if(($process -ne $null) -and ($process.GetType().Name -ne "Nothing"))
{
if(($process.Name.contains("HealthService") -Or $process.Name.contains("MonitoringHost")) -And (-Not($agentProcIDs.contains($("|" + $process.ProcessId + "|")))))
{
$agentProcIDs = $($agentProcIDs + $process.ProcessId + "|")
}
}
}
# Step 3: Get all the child processes
$childFound = $true
# While a new child is found, re-iterate the list to find its child
DO
{
$childFound = $false
foreach($process in $processes)
{
if(($process -ne $null) -and ($process.GetType().Name -ne "Nothing"))
{
# If parent process is in the agentProcIDs list but the process itself is not, its a new child
# Iterate each process to add the percent processor time to the to
foreach($process in $wmiService)
{
if($agentProcIDs.Contains($("|" + $process.IDProcess + "|")))
{
$x = $(GetProcessorTime $process.IDProcess $ComputerName)
$totalPercentProcessorTime = $totalPercentProcessorTime + $x
if($ConfigForRun -eq $true)
{
$procTime = $x
$sampleCount = $sampleCount + 1
$procTime = [double]$procTime
$totalCount = $totalCount + $procTime
# Check for min value
if($procTime -lt $min)
{
$min = $procTime
}
# Check for max value
if($procTime -gt $max)
{
$max = $procTime
}
}
}
}
# Add the total percentage time to the final percentage time for averaging in the end
$finalPercentProcessorTime = $finalPercentProcessorTime + $totalPercentProcessorTime
$dataCount = $dataCount + 1
break
}
}
# Delaying each iteration by 3 seconds
Start-Sleep 3
}
# Add the detailed analysis to the property bag
if($ConfigForRun -eq $true)
{
$oPropertyBag.AddValue("SamplesTaken", $ProcessIterationCount)
$oPropertyBag.AddValue("Average", [double]($totalCount/$sampleCount))
$oPropertyBag.AddValue("Maximum", $max)
$oPropertyBag.AddValue("Minimum", $min)
}
# Calculate the final percentage processor time for all the SCOM processes
if($dataCount -lt 1)
{
$dataCount = 1
}
$finalPercentProcessorTime = ($finalPercentProcessorTime/$dataCount)/$procCount
}
else
{
$message = $("Can't initialize the com object")
$oAPI.LogScriptEvent($SCRIPT_NAME, $SCRIPT_EVENT_ID, $CN_SCOM_ERROR, $message)
}
if($ConfigForRun -eq $false)
{
$oPropertyBag.AddValue("SCOMpercentageCPUTime", $finalPercentProcessorTime)
}
$oPropertyBag