Microsoft.SystemCenter.Omonline.OutsideIn.VSWebTest.SummaryMap.VSWebTestToGrouper.Script.ps1 (Resource)

Element properties:

TypeResource
File NameVSWebTestToGrouper.ps1
AccessibilityInternal

Source Code:

<Resource ID="Microsoft.SystemCenter.Omonline.OutsideIn.VSWebTest.SummaryMap.VSWebTestToGrouper.Script.ps1" Accessibility="Internal" FileName="VSWebTestToGrouper.ps1"/>

File Content: VSWebTestToGrouper.ps1

#************************************************************************************************************

# This script returns a collection of web tests for the group by location input.
#
# Parameters:
#
# Collection of IDO with the ID of the GroupByLocation instances. The output location is set to the
# DisplayName of these groups (which should also be given as input on the same IDO).
#
#************************************************************************************************************

Param(
$TargetEntityId,
$PublicKeyToken)

###############Constants###########################

###############Help Functions###########################
#
#
function Get-Type (
$type = $(throw "needs to speficy a type")
)
#-------------------------------------------------------
# Helper to get the right generic type created.
#
# Arguments:
# type - the string with the type name.
# args - array of arguments or argumens list
#
# Returns:
# The matching array of types.
#-------------------------------------------------------
{
trap [System.Management.Automation.RuntimeException] { throw ($_.Exception.Message) }

$returnType = $null;

if($args -and $args.Count -gt 0)
{
$types = $args[0] -as [type[]];
if(-not $types)
{
$types = [type[]] $args;
}

$genericType = [type] ($type + '`' + $types.Count);
if($genericType -ne $null)
{
$returnType = $genericType.MakeGenericType($types);
}
}
else
{
$returnType = [type] $type;
}

return $returnType;
}

function New-GenericObject(
$type = $(throw "needs to speficy a type"),
[object[]] $typeParameters = $null,
[object[]] $constructorParameters = @()
)
#-------------------------------------------------------
# Helper to instantiate a generic type.
#
# Arguments:
# type - the type (closed one, or a string to be closed).
# typeParameters - generic types to create generic, if not spefified or null then closed type assumed in input.
# constructors - parameters for constructor if not using the default one
#
# Returns:
# The matching array of types.
#-------------------------------------------------------
{
$closedType = (Get-Type $type $typeParameters)
,[Activator]::CreateInstance($closedType, $constructorParameters)
}

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
)

$paramsTypes = new-object object[] $params.Length;
$idx = 0;
foreach($p in $params)
{
$paramsTypes[$idx] = $params[$idx].GetType();
$idx = $idx + 1;
}

return $paramsTypes;
}

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
)

$method = $myType.GetMethod($myMethod, $myMethodArgumentTypes);
$genericMethod = $method.MakeGenericMethod($genericTypes);

$ret = $genericMethod.Invoke($object, $params);

return ,$ret;
}

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
)

$paramsTypes = Get-ParametersTypes($params);

$ret = Invoke-GenericMethod-WithTypes `
$myType `
$myMethod `
$paramsTypes `
$genericTypes `
$object $params;

return ,$ret;
}

#
#
###############Help Functions###########################

function Initialize-Script
#-------------------------------------------------------
# Initialization for testing.
#-------------------------------------------------------
{
if ($global:scriptContext -eq $null)
{
Write-Error "Missing the proper context argumet.";
exit;
}
}

function Initialize-SDK
#-------------------------------------------------------
# Initialize any objects we need from the SDK
#-------------------------------------------------------
{
# The mg
$script:mg = $global:scriptContext.ManagementGroup;

# Read/Cache SDK types...

$mp = $script:mg.ManagementPacks.GetManagementPack("Microsoft.SystemCenter.Omonline.OutsideIn.VSWebTest", $global:publicKeyToken, $null);
$script:grouperByLocationIsMemberOfEntity = $script:mg.EntityTypes.GetRelationshipClass("Microsoft.SystemCenter.Omonline.OutsideIn.VSWebTest.VSWebTestGrouperByLocation.IsMemberOf.Entity", $mp);
$script:webTestIsMemberOfAggregator = $script:mg.EntityTypes.GetRelationshipClass("Microsoft.SystemCenter.Omonline.OutsideIn.VSWebTest.VSWebTest.IsMemberOf.VSWebTestAggregator", $mp);
}

function Fetch-Grouper
#-------------------------------------------------------
# Creates the initial fetch of grouper.
#-------------------------------------------------------
{
$grouper = $null;

$params = new-object object[] 5;
$params[1] = $script:webTestIsMemberOfAggregator;
$params[2] = [Microsoft.EnterpriseManagement.Configuration.DerivedClassTraversalDepth]::None;
$params[3] = [Microsoft.EnterpriseManagement.Common.TraversalDepth]::OneLevel;
$params[4] = [Microsoft.EnterpriseManagement.Common.ObjectQueryOptions]::Default;

$genericTypes = new-object type[] 1;
$genericTypes[0] = [Microsoft.EnterpriseManagement.Monitoring.MonitoringObject];

$params[0] = $global:targetEntityId;
$list = Invoke-GenericMethod `
$script:mg.EntityObjects.GetType() `
"GetRelationshipObjectsWhereTarget" `
$genericTypes `
$script:mg.EntityObjects `
$params;

if ($list -eq $null)
{
Write-Error "could not go from VSWebTest to VSWebTestContainer!";
exit;
}

foreach($rel in $list)
{
$vsWebTestContainer = $rel.SourceObject;

# Now go to grouper...
$params[0] = $vsWebTestContainer.Id;
$params[1] = $script:grouperByLocationIsMemberOfEntity;
$listToGrouper = Invoke-GenericMethod `
$script:mg.EntityObjects.GetType() `
"GetRelationshipObjectsWhereSource" `
$genericTypes `
$script:mg.EntityObjects `
$params;

if ($listToGrouper -eq $null)
{
Write-Error "could not go from VSWebTestContainer to Grouper!";
exit;
}

foreach($relToGrouper in $listToGrouper)
{
$grouperObj = $relToGrouper.TargetObject;

$vd = New-GenericObject System.Collections.Generic.List Microsoft.EnterpriseManagement.Monitoring.DataProviders.ValueDefinition;

$value = new-object ([Microsoft.EnterpriseManagement.Monitoring.DataProviders.ValueDefinition]);
$value.OutputPropertyName = "Id";
$value.XPath = '$Object/Property[Name=''Id'']$';
$vd.Add($value);

$value = new-object ([Microsoft.EnterpriseManagement.Monitoring.DataProviders.ValueDefinition]);
$value.OutputPropertyName = "DisplayName";
$value.XPath = '$Object/Property[Name=''DisplayName'']$';
$vd.Add($value);

$grouper = $global:scriptContext.CreateFromObject($grouperObj, $vd);

# make sure we only have one item in the returned collection.
$global:scriptContext.ReturnCollection.Clear();

$global:scriptContext.ReturnCollection.Add($grouper);

break;
}

break;
}

break;
}

function Main
#-------------------------------------------------------
# Main entry point.
#-------------------------------------------------------
{
if ($TargetEntityId -eq $null)
{
Write-Error "The parameter TargetEntityId is null.";
exit;
}

if ($PublicKeyToken -eq $null)
{
Write-Error "The parameter PublicKeyToken is null.";
exit;
}

$global:targetEntityId = $TargetEntityId;

$global:publicKeyToken = $PublicKeyToken;

Initialize-Script;

Initialize-SDK;

Fetch-Grouper;
}

Main