Test/test011.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
BEGIN
{
    # get the Out-TestLog function
    . ./Common
    # import common functions (get-lorem)
    . ../DataGen/Common.ps1
    $TESTNAME = $MyInvocation.MyCommand
    $name = "System.WorkItem.Incident"
    $STARTTIME = [datetime]::Now

    $CClass = get-scsmclass -name "microsoft.windows.computer$"
    $includeList = @(get-scsmobject -class $CClass -MaxCount 3)
    $excludeList = @(get-scsmobject -class (get-scsmclass -name system.printer) -max 2)
    $GroupName = (Get-Lorem 3) -replace " "
    $TESTFAILED = $FALSE
    $SLEEPSECONDS = 60
    $MSG = "Sleeping for $SLEEPSECONDS seconds for group calc execution" 
    if ( ! $includeList -or ! $excludeList )
    {
        Out-TestLog ("FAIL: " + [datetime]::Now + ":${TESTNAME}001")
        Out-TestLog (" DETAILS: IncludeList Count = " + $includeList.Count + " ExcludeList Count = " + $excludeList.Count)
        1
        exit
    }
}

END
{

    ### TEST 011a
    try
    {
        # Test with include and exclude where include and exclude are
        # different classes but all objects within include and exclude
        # are the same class
        # This doesn't actually import the management pack, so no reason to remove it
        # so only check the group configuration
        $GroupArgs = @{
            Include            = $includeList
            ManagementPackName = "TESTMP1"
            Name               = $GroupName
            Description        = "A description for you!" 
            Exclude            = $excludeList
            PassThru           = $true
            }
        $MP = new-scgroup @GroupArgs
        $INCLUDEXPATH = "ManagementPack/Monitoring/Discoveries/Discovery/DataSource/MembershipRules/MembershipRule/IncludeList/MonitoringObjectId"
        $EXCLUDEXPATH = "ManagementPack/Monitoring/Discoveries/Discovery/DataSource/MembershipRules/MembershipRule/ExcludeList/MonitoringObjectId"
        $groupIncludeMembers = ([xml]$MP.getxml()).SelectNodes($INCLUDEXPATH)|%{$_."#text"}|sort
        $groupExcludeMembers = ([xml]$MP.getxml()).SelectNodes($EXCLUDEXPATH)|%{$_."#text"}|sort
        
        # $groupIncludeMembers = $g.IncludeList |%{$_.id}|sort
        # $groupExcludeMembers = $g.ExcludeList |%{$_.id}|sort
        $includeListGuids = $includeList|%{$_.id}|sort
        $excludeListGuids = $excludeList|%{$_.id}|sort
        if ( (compare-object $groupIncludeMembers $includeListGuids) -or (Compare-Object $groupExcludeMembers $excludeListGuids))
        {
            $TESTFAILED = $true
            Out-TestLog ("FAIL: " + [datetime]::Now + ":${TESTNAME}011a")
        }
        else
        {
            Out-TestLog ("PASS: " + [datetime]::Now + ":${TESTNAME}011a")
        }
    }
    catch
    {
        $TESTFAILED = $true
        Out-TestLog ("FAIL: " + [datetime]::Now + ":${TESTNAME}011a")
        $error | %{ Out-TestLog (" DETAIL: " + $_) }
    }
    # no MP imported - test complete

    ### TEST 011b
    try
    {
        ## The simplest group test
        $MANAGEMENTPACKNAME = "TESTMP2"
        $error.clear()
        # clear the include and exclude lists
        # we'll pass the include list in the pipeline
        $GroupArgs.Remove("Include")
        $GroupArgs.Remove("Exclude")
        $GroupArgs.ManagementPackName = $MANAGEMENTPACKNAME
        $GroupArgs.Import = $true
        $MP = $includeList | new-scgroup @GroupArgs
        for($i = 0; $i -le $SLEEPSECONDS; $i+=5)
        {
            Write-Progress -Activity $msg -Status $i -perc ( 100 * ($i/$SLEEPSECONDS))
            start-sleep 5
        }
        $Group = Get-SCGroup -DisplayName $GroupName
        $includeListGuids = $includeList | %{ $_.id } | sort
        $groupIncludeMembers = $Group.Members | %{ $_.id } | sort
        if ( Compare-Object $includeListGuids $groupIncludeMembers )
        {
            $TESTFAILED = $true
            Out-TestLog ("FAIL: " + [datetime]::Now + ":${TESTNAME}011b")
        }
        else
        {
            Out-TestLog ("PASS: " + [datetime]::Now + ":${TESTNAME}011b")
        }
    }
    catch
    {
        $TESTFAILED = $true
        Out-TestLog ("FAIL: " + [datetime]::Now + ":${TESTNAME}011b" )
        $error | %{ Out-TestLog (" DETAIL: " + $_ ) }
    }
    finally
    {
        Get-SCManagementPack -Name $MANAGEMENTPACKNAME | Remove-SCManagementPack
    }

    #
    # Test 3 include are multiple classes
    try
    {
        $MANAGEMENTPACKNAME = "TESTMP3"
        $GroupArgs.Include = @( $includelist; $excludeList )
        $GroupArgs.Import  = $true
        $GroupArgs.ManagementPackName = $MANAGEMENTPACKNAME
        $MP = new-scgroup @GroupArgs
        for($i = 0; $i -lt $SLEEPSECONDS; $i+=5)
        {
            Write-Progress -Activity $msg -Status $i -perc ( 100 * ($i/$SLEEPSECONDS))
            start-sleep 5
        }
        start-sleep $SLEEPSECONDS
        $Group = Get-SCGroup -DisplayName $GroupName
        $ListGuids = @( $includeList; $excludeList) | %{ $_.id } | sort
        $groupIncludeMembers = $Group.Members | %{ $_.id } | sort
        if ( (compare-object $groupIncludeMembers $ListGuids))
        {
            $TESTFAILED = $true
            Out-TestLog ("FAIL: " + [datetime]::Now + ":${TESTNAME}011c")
            $groupIncludeMembers | %{ Out-TestLog (" Details: includemember - $_" ) }
            $ListGuids | %{ Out-TestLog (" Details: ListGuid - $_" )  }
        }
        else
        {
            Out-TestLog ("PASS: " + [datetime]::Now + ":${TESTNAME}011c")
        }
    }
    catch
    {
        $TESTFAILED = $true
        Out-TestLog ("FAIL: " + [datetime]::Now + ":${TESTNAME}011c")
        $error | %{ Out-TestLog (" DETAIL: " + $_) }
    }
    finally
    {
        # ok - this MP was imported, so remove it
        Get-SCManagementPack -Name $MANAGEMENTPACKNAME | Remove-SCManagementPack
    }

    if ( $TESTFAILED ) { return 1 } else { return 0 }
}