Scripts/get-singlepropertycriteria.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

param ( 
    [Parameter(Mandatory=$true,Position=0)][Microsoft.EnterpriseManagement.Configuration.ManagementPackProperty]$property,
    [Parameter(Mandatory=$true,Position=1)][string]$op,
    [Parameter(Mandatory=$true,Position=2)][string]$value,
    [switch]$stringOnly
    )
# Create the expression XML...something like:
#<Criteria xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/">
# <Reference Id="ObjectInteractionClient" Version="1.0.0.0" Alias="Test" />
# <Expression>
# <SimpleExpression>
# <ValueExpressionLeft>
# <Property>$Target/Property[Type='Test!ObjectInteractionClient.Bleh']/BlehId$</Property>
# </ValueExpressionLeft>
# <Operator>Equal</Operator>
# <ValueExpressionRight>
# <Value>JakubTestIdCompositeTests</Value>
# </ValueExpressionRight>
# </SimpleExpression>
# </Expression>
#</Criteria>

function New-Criteria ( $stringBuilder, $MP, [switch]$UseNamespaces, $Namespace )
{
    $ref1 = "myMP"
    $references.Add($ref1, $MP)
    $criteriaWriter = new-object io.StringWriter $stringBuilder 
    $criteriaXmlWriter = new-object System.Xml.XmlTextWriter $criteriaWriter
    $criteriaXmlWriter.Namespaces = $true
    $criteriaXmlWriter.WriteStartElement("Criteria", $NameSpace)
    $criteriaXmlWriter.WriteStartElement("Reference", $NameSpace)
    $criteriaXmlWriter.WriteAttributeString("Id", $MP.Name)
    $criteriaXmlWriter.WriteAttributeString("Version", $MP.Version.ToString())
    $criteriaXmlWriter.WriteAttributeString("PublicKeyToken", $MP.KeyToken)
    $criteriaXmlWriter.WriteAttributeString("Alias", $ref1)
    $criteriaXmlWriter.WriteEndElement()
    $criteriaXmlWriter
}

function Add-Expression ( $XMLWRITER, $NS, $PP, $OP, $VALUE )
{
    write-host -for red $PP
    $XMLWRITER.WriteStartElement("Expression", $NS)
    $XMLWRITER.WriteStartElement("SimpleExpression", $NS)
    $XMLWRITER.WriteStartElement("ValueExpressionLeft", $NS)
    $XMLWRITER.WriteElementString("Property", $NS, $PP)
    $XMLWRITER.WriteEndElement() # ValueExpressionLeft
    $XMLWRITER.WriteElementString("Operator", $NS, $OP)
    $XMLWRITER.WriteStartElement("ValueExpressionRight", $NS)
    $XMLWRITER.WriteElementString("Value", $NS, $VALUE)
    $XMLWRITER.WriteEndElement() # ValueExpressionRight
    $XMLWRITER.WriteEndElement() # SimpleExpression
    $XMLWRITER.WriteEndElement() # Expression
}

$genericTypeName = "System.Collections.Generic.Dictionary``2"
$genericType = [type]$genericTypeName
[type[]] $typedParameters = "string","Microsoft.EnterpriseManagement.Configuration.ManagementPack"
$closedType = $genericType.MakeGenericType($typedParameters)
$global:references = [activator]::CreateInstance($closedType, $null)

$MPCLASS = [microsoft.enterprisemanagement.configuration.managementpackclass]
$SPE = "Microsoft.EnterpriseManagement.Common.SeedPathElement"
$PPE = "Microsoft.EnterpriseManagement.Common.PropertyPathElement"
$mpp = [Microsoft.EnterpriseManagement.Configuration.ManagementPackProperty]$p
$global:seed = new-object $SPE $mpp.parentelement
$seed.ChildElement = new-object $PPE $property


$global:criteriaBuilder = new-object text.stringbuilder
$ExpressionNamespace = "http://Microsoft.EnterpriseManagement.Core.Criteria/"
# This returns an XML writer with the header stuff already done
$propertyMP = $property.GetManagementPack()
$criteriaXmlWriter = New-Criteria -string $criteriaBuilder -mp $propertyMP -usen $true -namespace $ExpressionNamespace

$propertyPath = $seed.ToString($references) -replace "Context","Target"
Add-Expression $criteriaXmlWriter $ExpressionNamespace $PropertyPath $op $value

$criteriaXmlWriter.WriteEndElement() # Criteria
$criteriaXmlWriter.Flush()

$cString = $criteriaBuilder.ToString()
if ( $stringOnly ) { $cstring } 
else
{
$CRITERIATYPE = "Microsoft.EnterpriseManagement.Common.EnterpriseManagementObjectCriteria"
new-object $CRITERIATYPE $cString,$property.ParentElement,$property.ManagementGroup
}