foreach ($MEObject in $CounterQueryInfo["ManagedEntityIds"])
{
$scopingEntities.Add($MEObject);
}
if ($scopingEntities.Length -eq 0)
{
Write-Host "Scoping entity list cannot be empty!"
exit;
}
$firstMEObject = $scopingEntities[0];
if ($firstMEObject -ne $null)
{
$scopingEntityId = $firstMEObject["Id"]
}
else
{
Write-Host "First managed entity in the counter query info cannot be null!"
exit;
}
# Input parameters
# Standard input
$scriptContext = $global:scriptContext
function Get-ParametersTypes
#-------------------------------------------------------
# Helper to generate an array of parameter types
# from the actual parameters. Note that it won' work
# if passed argument is null or contain a null value
# in its content.
#
# Arguments:
# params - array of arguments.
#
# Returns:
# The matching array of types.
#-------------------------------------------------------
{
param (
[object[]]$params = $null
)
function Invoke-GenericMethod-WithTypes
#-------------------------------------------------------
# Makes a generic and call it returning whatever it
# returns
#
# Arguments:
# myType - Type containing the generic method.
# myMethod - Method name
# myMethodArgumentTypes - Argument types for the method (support overload)
# genericTypes - Generic passing in types
# object - Object to call the generic on
# params - Parameters for the call
#
# Returns:
# The matching array of types.
#-------------------------------------------------------
{
param (
[type]$myType,
[string]$myMethod,
[type[]]$myMethodArgumentTypes,
$genericTypes,
$object,
[object[]]$params = $null
)
function Invoke-GenericMethod
#-------------------------------------------------------
# Makes a generic and call it returning whatever it
# returns
#
# Arguments:
# myType - Type containing the generic method.
# myMethod - Method name
# genericTypes - Generic passing in types
# object - Object to call the generic on
# params - Parameters for the call
#
# Returns:
# The matching array of types.
#-------------------------------------------------------
{
param (
[type]$myType,
[string]$myMethod,
$genericTypes,
$object,
[object[]]$params = $null
)
function PrepareMEListXml
#-------------------------------------------------------
# This method prepares an xml string that contains the
# ids of all the instances of the target type in the
# specified group.
#
# Arguments:
# n/a
#
# Returns:
# n/a
#-------------------------------------------------------
{
# Write the book element.
[void]$writer.WriteStartElement("ManagedEntityIds");
# Add the contained elements.
foreach ($obj in $queryIds)
{
# Write the title element.
[void]$writer.WriteStartElement("Id");
[void]$writer.WriteString([Guid]$obj);
[void]$writer.WriteEndElement();
}
# Write the close tag for the root element.
[void]$writer.WriteEndElement();
# Write the XML and close the writer.
[void]$writer.Close();
function InitializeStartEndTimes
#-------------------------------------------------------
# This method sets the start and end times of the query
# using the passed in time interval object.
#
# Arguments:
# n/a
#
# Returns:
# n/a
#-------------------------------------------------------
{
$startObj = $TimeInterval["StartPoint"];
$startBase = $startObj["BaseDateTime"].ToString();
$startOffsetType = $startObj["OffsetType"].ToString();
$startOffset = $startObj["Offset"].ToString();
function InitializeParameters
#-------------------------------------------------------
# This method initializes the parameters
# using the mp name and the type name in the type
# parameter's FullyQualifiedName field.
#
# Arguments:
# n/a
#
# Returns:
# n/a
#-------------------------------------------------------
{
InitializeStartEndTimes;
PrepareMEListXml;
# set order by value
if ($ReturnBottomN -eq 1)
{
$script:orderByParamValue = 0;
}
else
{
$script:orderByParamValue = 1;
}
#set aggregation type
$timeSpan = $endDateTime.Subtract($startDateTime);
function SetOutput
#-------------------------------------------------------
# This method adds the entities returned from the DW
# to the return collection.
#
# Arguments:
# n/a
#
# Returns:
# n/a
#-------------------------------------------------------
{
$newEntityList = @()
if ($script:rs -eq $null)
{
throw ("Result set is null!")
}
#Calculate the new entity collection
foreach($result in $rs.Results)
{
if ($result -eq $null)
{
throw ("Result is null")
}
if ($result.Values -eq $null)
{
throw ("Result.Values is null")
}
# Get the MonitoringObject with the Id we get from db.
$me = Get-SCOMClassInstance -Id $result.Values[0]
# Create an IDataObject from the MonitoringObject with properties Id and DisplayName
$dataObject = $scriptContext.CreateFromObject($me, "Id=Id,DisplayName=DisplayName,Path=ExtendedPath", $null)