Test/test-newproblem.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
param ( $count = 10 , [switch]$whatif, [switch]$Verbose, [switch]$debug)

BEGIN
{

    function Get-Priority
    {
        param ( [string]$myurgency, [string]$myimpact )
        $hunt = "${myurgency}${myimpact}"
        $PriorityData = (get-scsmobject -class (get-scsmclass -name System.WorkItem.Incident.GeneralSetting)).PriorityMatrix
        if ( $PriorityData )
        {
            $matrix = [xml]$PriorityData
        }
        $Impact  = Get-SCSMEnumeration System.WorkItem.TroubleTicket.ImpactEnum.|sort-object Ordinal
        $Urgency = Get-SCSMEnumeration System.WorkItem.TroubleTicket.UrgencyEnum.|sort-object Ordinal
        $hash = @{}
        $count=1
        foreach($U in $Urgency)
        {
            foreach($I in $Impact)
            {
                $UN = $U.DisplayName; $IN = $I.DisplayName
                $UID = $U.ID; $IID = $I.ID
                $xpath = "Matrix/U[@Id='$UID']/I[@Id='$IID']/P"
                if ( $ProrityData )
                {
                    $value = $Matrix.SelectSingleNode($xpath)."#text"
                }
                else
                {
                    $Value = $count++
                }
                $hash["${UN}${IN}"] = $value
            }
        }
        $hash[$hunt]
    }

    function new-FileAttachmentStream
    {
        param ( $count = 3 )
        $OPEN = ([io.filemode]::Open)
        $RACC = ([io.fileaccess]::Read)
        $RSHR = ([io.fileshare]::read)
        for($i = 0; $i -lt $count; $i++)
        {
            $DOCUMENT = [io.path]::GetTempFileName()
            get-loremipsum 60 > $DOCUMENT
            new-object io.filestream $DOCUMENT,$OPEN,$RACC,$RSHR
        }
    }

    function get-loremipsum
    {
        param ( [int]$count = 11 )
        #$s = $Start.ToString().ToLower()
        #$URL = �"http://www.lipsum.com/feed/xml?amount=${count}&what=words&start=${s}"
        #[xml]$x = ((new-object net.webclient).downloadstring($URL))
        #$x.feed.lipsum

        $words = "consetetur","sadipscing","elitr","sed","diam","nonumy","eirmod","tempor","invidunt","ut","labore","et","dolore","magna","aliquyam","erat","sed","diam","voluptua","at","vero","eos","et","accusam","et",
            "justo","duo","dolores","et","ea","rebum","stet","clita","kasd","gubergren","no","sea","takimata","sanctus", "est","lorem","ipsum","dolor","sit","amet","lorem","ipsum","dolor","sit","amet","consetetur","sadipscing",
            "elitr","sed","diam","nonumy","eirmod","tempor","invidunt","ut","labore","et","dolore","magna","aliquyam", "erat","sed","diam","voluptua","at","vero","eos","et","accusam","et","justo","duo","dolores","et","ea",
            "rebum","stet","clita","kasd","gubergren","no","sea","takimata","sanctus","est","lorem","ipsum","dolor", "sit","amet","lorem","ipsum","dolor","sit","amet","consetetur","sadipscing","elitr","sed","diam","nonumy",
            "eirmod","tempor","invidunt","ut","labore","et","dolore","magna","aliquyam","erat","sed","diam","voluptua", "at","vero","eos","et","accusam","et","justo","duo","dolores","et","ea","rebum","stet","clita","kasd",
            "gubergren","no","sea","takimata","sanctus","est","lorem","ipsum","dolor","sit","amet","duis","autem","vel", "eum","iriure","dolor","in","hendrerit","in","vulputate","velit","esse","molestie","consequat","vel","illum",
            "dolore","eu","feugiat","nulla","facilisis","at","vero","eros","et","accumsan","et","iusto","odio", "dignissim","qui","blandit","praesent","luptatum","zzril","delenit","augue","duis","dolore","te","feugait",
            "nulla","facilisi","lorem","ipsum","dolor","sit","amet","consectetuer","adipiscing","elit","sed","diam", "nonummy","nibh","euismod","tincidunt","ut","laoreet","dolore","magna","aliquam","erat","volutpat","ut",
            "wisi","enim","ad","minim","veniam","quis","nostrud","exerci","tation","ullamcorper","suscipit","lobortis", "nisl","ut","aliquip","ex","ea","commodo","consequat","duis","autem","vel","eum","iriure","dolor","in",
            "hendrerit","in","vulputate","velit","esse","molestie","consequat","vel","illum","dolore","eu","feugiat", "nulla","facilisis","at","vero","eros","et","accumsan","et" 
         
        $RANDOM = new RANDOM
        [byte[]]$b = new byte[] $count
        $RANDOM.NextBytes($b)
        ($words[$b] -join " ").Trim() + "."
    }

    function Get-RandomItemFromList
    {
        param ( [Parameter(Mandatory=$true,Position=0)]$list )
        $list[$RANDOM.Next(0,$list.Count)]
    }

    function Get-RandomListFromList
    {
        param ( 
            [Parameter(Mandatory=$true,Position=0)]$list, 
            [Parameter(Mandatory=$true,Position=1)][int]$count
            )
        $mylist = [Collections.ArrayList]$list
        $RandomList =  @()
        for($i = 0; $i -lt $count -and $mylist.Count -gt 0; $i++)
        {
            $r = $RANDOM.Next(0,$mylist.Count)
            $RandomList += $mylist[$r]
            $mylist.RemoveAt($r)
        }
        $RandomList
    }

    $RANDOM = new-object System.Random
    $PTYPE = "System.WorkItem.Problem.ProjectionType"
    ###
    ### SETUP
    ### Retrieve stuff from the CMDB which will be used later
    ###
    Write-Progress -Activity "Setting Up Environment" -Status "Getting Users"
    $Users = Get-scsmobject -class (get-scsmclass -name Microsoft.AD.User$) -MaxCount 60
    if ( $Users.Count -lt 20 ) {
        Write-Error "Not enough users, go make some more"
        exit
        }
    Write-Progress -Activity "Setting Up Environment" -Status "Getting Config Items"
    $CIList = get-scsmobject -class (get-scsmclass -name System.ConfigItem$) -MaxCount 60
    if ( $CIList.Count -lt 20 ) {
        Write-Error "Not enough CIs, go make some more"
        exit
        }

    Write-Progress -Activity "Setting Up Environment" -Status "Getting Enumerations"
    # ENUMERATIONS
    # the "." at the end of the enumeration is required to be sure we get the list
    $StatusList         = Get-SCSMEnumeration ProblemStatusEnum.
    $SourceList         = get-scsmenumeration ProblemSourceEnum.
    $ResolutionList     = Get-SCSMEnumeration ProblemResolutionEnum.
    $ClassificationList = Get-SCSMEnumeration ProblemClassificationEnum.
    $ImpactList         = Get-SCSMEnumeration System.WorkItem.TroubleTicket.ImpactEnum.
    $UrgencyList        = Get-SCSMEnumeration System.WorkItem.TroubleTicket.UrgencyEnum.
    
    Write-Progress -Activity "Setting Up Environment" -Status "Starting Problem Creation"
}

END
{
    1..$count|%{
        $i = $_
        Write-Progress -Activity "Creating Problem" -Status $i -perc ([int]($i/$count * 100))

        # This is the date
        $CreatedDate = [datetime]::Now.AddDays(-$RANDOM.Next(30,90))

        $ACIs = Get-RandomListFromList $CIList 5 # $CIList[$rlist]
        $RCIs = Get-RandomListFromList $CIList 4 # $CIList[$rlist]

        $Impact         = Get-RandomItemFromList $ImpactList
        $Urgency        = Get-RandomItemFromList $UrgencyList
        $Priority       = Get-Priority $Urgency.DisplayName $Impact.DisplayName
        $Status         = Get-RandomItemFromList $StatusList
        # DEBUG::: $Status = $StatusList | ?{$_.Name -match "Resolv"}
        $Source         = Get-RandomItemFromList $SourceList
        $Classification = Get-RandomItemFromList $ClassificationList
        $Resolution     = Get-RandomItemFromList $ResolutionList


        if     ( $Urgency -match "high" )   { $TargetResolutionTime = $CreatedDate.AddHours(4)  }
        elseif ( $Urgency -match "medium" ) { $TargetResolutionTime = $CreatedDate.AddHours(24) }
        elseif ( $Urgency -match "high" )   { $TargetResolutionTime = $CreatedDate.AddHours(72) }
        else                                { $TargetResolutionTime = $CreatedDate.AddHours(48) }

        $AffectedUser  = Get-RandomItemFromList $users
        $AssignedUser  = Get-RandomItemFromList $users
        $CreatedByUser = Get-RandomItemFromList $users
        $ResolvedBy    = Get-RandomItemFromList $users
        $ClosedBy      = Get-RandomItemFromList $users

        # by default this creates 5
        # $global:FASTREAMS = new-FileAttachmentStream

        # CREATE THE SEED HASH TABLE
        $ProblemSeed = @{
                Id                   = "CustomProblem{0}"
                Urgency              = $Urgency
                Impact               = $Impact
                Status               = $Status
                Source               = $Source
                Classification       = $Classification
                Resolution           = $Resolution
                Priority             = $Priority
                # ClosedDate
                # ResolvedDate
                Title                = get-loremipsum 6
                Description          = get-loremipsum 22
                CreatedDate          = $CreatedDate
                KnownError           = [bool]($random.Next(0,2))
                RequiresMajorProblemReview = [bool]($random.Next(0,2))
            }

        # FINISH THE SEED HASH TABLE
        # set up for closed and resolved status
        if ( $status -match "Closed" -or $status -match "Resolved" )
        {
            $ProblemSeed['ResolutionDescription'] = get-lorem 22
            $ProblemSeed['ResolutionCategory']    = Get-RandomItemFromList $ResolutionList
            $ResolveDiff = ($TargetResolutionTime - $CreatedDate).TotalHours
            $TimeVariance = $RANDOM.Next(-$ResolveDiff,$ResolveDiff)
            $ResolvedDate = $TargetResolutionTime.AddHours($TimeVariance)
            $ProblemSeed['ResolvedDate'] = $ResolvedDate
            if ( $status -match "Closed" )
            {
                $ProblemSeed['ClosedDate'] = $ResolvedDate.AddHours($RANDOM.Next(0,2)*24)
            }
            
        }

        $p = @{
            __CLASS = "System.WorkItem.Problem"
            __OBJECT = $ProblemSeed
            # Now for the Aliases
            AssignedTo             = $AssignedUser
            CreatedBy            = $CreatedByUser
            AffectedConfigItems      = $ACIs
            }
        if ( $status -match "Resolved" ) { $p['ResolvedBy'] = $ResolvedBy }
        if ( $status -match "Closed" )   { $p['ResolvedBy'] = $ResolvedBy; $p['ClosedBy'] = $ClosedBy }

    $p } | new-SCSMOBjectProjection -Type $PType -bulk -verbose:$verbose -whatif:$whatif -debug:$debug

}