#************************************************************************************************************
# This script returns a collection of Visual Studio 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).
#
#************************************************************************************************************
function GetTimeUtc (
$time = $(throw "needs to speficy a type")
)
#-------------------------------------------------------
# Helper to convert SDK returned times to UTC.
#
# Arguments:
# time - the time that might be unspecified timezone
#
# Returns:
# time with utc kind set
#-------------------------------------------------------
{
if ($time -ne $null)
{
return new-object ([DateTime])($time.Ticks, [DateTimeKind]::Utc);
}
return $time;
}
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) }
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
)
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 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;
function Populate-LastResponseTime
#-------------------------------------------------------
# Creates the initial fetch of web test objects.
#-------------------------------------------------------
{
# No tests, nothing to do
if ($script:webTests.Count -eq 0)
{
return;
}
# need to find a target to go get the current DisplayName of the grouper.
$sampleWebTest = $null;
foreach ($target in $script:webTests.Values)
{
$sampleWebTest = $target;
break;
}
if ($sampleWebTest -eq $null)
{
return;
}
function Fetch-WebTestList
#-------------------------------------------------------
# Creates the initial fetch of web test objects.
#-------------------------------------------------------
{
$script:webTests = new-object ('System.Collections.Generic.Dictionary[Guid, Object]');
if ($list -eq $null)
{
Write-Error "could not go from GroupByLocation to WebTests!";
exit;
}
foreach($rel in $list)
{
$smo = $rel.SourceObject;
$mo = $rel.TargetObject;
# its ok to compare before setting Kind.
$mt = $mo.LastModified;
if ($mt -lt $mo.StateLastModified)
{
$mt = $mo.StateLastModified;
}
if ($mt -lt $mo.AvailabilityLastModified)
{
$mt = $mo.AvailabilityLastModified;
}
$webTest = $global:scriptContext.CreateFromObject($mo, $vd);
if ($webTest -eq $null)
{
Write-Error "webTest object cannot be instantiated.";
exit;
}
# this test is only needed becasue of the test script library,
# instead of using a local variable.
# We are using IsChecked already there means types have been created already.
$isCreated = $webTest.Type.Properties["IsChecked"];
if ($isCreated -eq $null)
{
[void]$webTest.Type.Properties.Create("IsChecked", [bool]);
[void]$webTest.Type.Properties.Create("Color", [Microsoft.EnterpriseManagement.Presentation.DataAccess.IDataObject]);
[void]$webTest.Type.Properties.Create("LastTransactionResponseTime", [double]);
[void]$webTest.Type.Properties.Create("LastSampleTime", [DateTime]);
[void]$webTest.Type.Properties.Create("Location", [String]);
[void]$webTest.Type.Properties.Create("ModifiedTime", [DateTime]);
}
function MergeForRefresh
#-------------------------------------------------------
# Will create or update the Returned collection.
#-------------------------------------------------------
{
$tests = new-object ('System.Collections.Generic.List[object]')($global:scriptContext.ReturnCollection);
$fetchWebTest = $null;
foreach($webTest in $tests)
{
$id = [Guid]$webTest["Id"];
if ($script:webTests.TryGetValue($id, [ref]$fetchWebTest))
{
$fetchTime = $fetchWebTest["ModifiedTime"];
$oldTime = $webTest["ModifiedTime"];
# flag that it is not to be added since it was modified or simply didn't change.
$script:webTests[$id] = $null;
}
else # remove the object
{
$global:scriptContext.ReturnCollection.Remove($webTest);
}
}
foreach($id in $script:webTests.Keys)
{
$webTest = $script:webTests[$id];
if ($webTest -ne $null)
{
$global:scriptContext.ReturnCollection.Add($webTest);
}
}
}
function Main
#-------------------------------------------------------
# Main entry point.
#-------------------------------------------------------
{
if ($TargetEntities -eq $null)
{
Write-Error "The parameter TargetEntities is null.";
exit;
}
if ($PublicKeyToken -eq $null)
{
Write-Error "The parameter PublicKeyToken is null.";
exit;
}