DataGen/Initialize-Change.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
param ( $count = 10 , [switch]$whatif, [switch]$Verbose, [switch]$debug)

BEGIN
{
    . ./Common.ps1

    $PTYPE = "System.WorkItem.ChangeRequestProjection"

    # RESOURCES NEEDED BUILT BY COMMON.PS1
    #
    # INSTANCEHASH[$UserClass]
    # INSTANCEHASH[$CIClass]
    # ENUMHASH["ProblemStatusEnum"]
    # ENUMHASH["ProblemSourceEnum"]
    # ENUMHASH["ProblemResolutionEnum"]
    # ENUMHASH["ProblemClassificationEnum"]
    # ENUMHASH["System.WorkItem.TroubleTicket.ImpactEnum"]
    # ENUMHASH["System.WorkItem.TroubleTicket.UrgencyEnum"]

    # Add to ENUMHASH
        "ChangeAreaEnum", "ChangeCategoryEnum", "ChangeImpactEnum",
        "ChangeImplementationResultsEnum", "ChangePriorityEnum",
        "ChangeRiskEnum", "ChangeStatusEnum" | %{
            $ENUMHASH[$_] = Get-EnumList $_
            }

    # Add to ENUMHASH for Activities
        "ApprovalEnum", "ActivityAreaEnum", "ActivityPriorityEnum",
        "ActivityStageEnum", "ActivityStatusEnum" | %{
            $ENUMHASH[$_] = Get-EnumList $_
            }

    $ENUMHASH['DecisionEnum'] = Get-EnumList DecisionEnum

    # INSTANCEHASH needs a System.WorkItem
    $INSTANCEHASH['System.WorkItem'] = Get-Instances "System.WorkItem" 60

    # SEED PROPERTIES
        #CreatedDate DateTime
        #ScheduledStartDate DateTime
        #ScheduledEndDate DateTime
        #ActualStartDate DateTime
        #ActualEndDate DateTime

    # Projection components
    # Activity System.WorkItem.Activity
    # CreatedBy System.User
    # AssignedTo System.User
    # AffectedUser System.User
    # RelatedWorkItem System.WorkItem
    # RelatedWorkItemSource System.WorkItem
    # RelatedConfigItem System.ConfigItem
    # AboutConfigItem System.ConfigItem
    # RelatedKnowledge System.Knowledge.Article
    

    
    Write-Progress -Activity "Setting Up Environment" -Status "Starting Problem Creation"
}

END
{
    # THIS MUST BE DONE BY FOREACH-OBJECT TO GET BULK OPERATIONS
    1..$count|foreach-object {
        $i = $_
        Write-Progress -Activity "Creating Problem" -Status $i -perc ([int]($i/$count * 100))
        # This is the created date for the projection
        $CreatedDate = [datetime]::Now.AddDays(-$RANDOM.Next(30,90))

        $ACIs = Get-RandomListFromList $INSTANCEHASH[$CIClass] 4

        $Status         = Get-RandomItemFromList $ENUMHASH['ActivityStatusEnum']
        $Classification = Get-RandomItemFromList $ENUMHASH['ProblemClassificationEnum']
        $Resolution     = Get-RandomItemFromList $ENUMHASH['ProblemResolutionEnum']
        $ResolvedBy     = Get-RandomItemFromList $INSTANCEHASH[$UserClass]
        $ClosedBy       = Get-RandomItemFromList $INSTANCEHASH[$UserClass]

        # CREATE THE SEED HASH TABLE
        # WorkItem.Problem
        $Seed = @{
            Id                        = "CustomCR{0}"
            Reason                    = Get-LoremIpsum 12
            Notes                     = Get-LoremIpsum 24
            ImplementationPlan        = Get-LoremIpsum 30
            RiskAssessmentPlan        = Get-LoremIpsum 40
            BackoutPlan               = Get-LoremIpsum 80
            TestPlan                  = Get-LoremIpsum 60
            PostImplementationReview  = Get-LoremIpsum 32
            Title                     = Get-LoremIpsum 8
            Description               = Get-LoremIpsum 32
            ContactMethod             = Get-LoremIpsum 5

            Category                  = Get-RandomItemFromList $ENUMHASH['ChangeCategoryEnum']
            Priority                  = Get-RandomItemFromList $ENUMHASH['ChangePriorityEnum']
            Impact                    = Get-RandomItemFromList $ENUMHASH['System.WorkItem.TroubleTicket.ImpactEnum']
            Risk                      = Get-RandomItemFromList $ENUMHASH['ChangeRiskEnum']
            ImplementationResults     = Get-RandomItemFromList $ENUMHASH['ChangeImplementationResultsEnum']
            Status                    = $Status
            CreatedDate               = $CreatedDate
            }
        $Activites = 1..3 | %{
            #set-psdebug -trace 2
            $Start = $CreatedDate.Addhours((Get-RandomPercentage))
            $End   = $Start.AddHours((Get-RandomPercentage)*2)
            #set-psdebug -off
            $activitySeed = @{
                ActualCost                     = Get-RandomDouble
                ActualDowntimeEndDate          = $end.AddMinutes((Get-RandomPercentage))
                ActualDowntimeStartDate        = $start.AddMinutes((Get-RandomPercentage))
                ActualEndDate                  = $end.AddMinutes((Get-RandomPercentage))
                ActualStartDate                = $start.AddMinutes((Get-RandomPercentage))
                ActualWork                     = Get-RandomDouble
                ApprovalCondition              = Get-RandomItemFromList $ENUMHASH['ApprovalEnum']
                ApprovalPercentage             = Get-RandomPercentage
                Area                           = Get-RandomItemFromList $ENUMHASH['ActivityAreaEnum']
                Comments                       = get-loremipsum 22
                ContactMethod                  = get-loremipsum 22
                CreatedDate                    = $date
                Description                    = get-loremipsum 22
                Documentation                  = Get-LoremIpsum
                Id                             = "CustomRA{0}"
                IsDowntime                     = Get-RandomBool
                IsParent                       = Get-RandomBool
                LineManagerShouldReview        = Get-RandomBool
                Notes                          = get-loremipsum 12
                OwnersOfConfigItemShouldReview = Get-RandomBool
                PlannedCost                    = Get-RandomDouble
                PlannedWork                    = Get-RandomDouble
                Priority                       = Get-RandomItemFromList $ENUMHASH['ActivityPriorityEnum']
                RequiredBy                     = $end
                ScheduledDowntimeEndDate       = $end
                ScheduledDowntimeStartDate     = $start
                ScheduledEndDate               = $End
                ScheduledStartDate             = $Start
                SequenceId                     = $_
                Skip                           = Get-RandomBool
                Stage                          = Get-RandomItemFromList $ENUMHASH['ActivityStageEnum']
                Status                         = Get-RandomItemFromList $ENUMHASH['ActivityStatusEnum']
                Title                          = get-loremipsum 12
                }
            $ActivityProjectionType = "System.WorkItem.Activity.ReviewActivityProjection"
            
            @{
                __CLASS = "System.WorkItem.Activity.ReviewActivity"
                __OBJECT = $seed
                ActivityCreatedBy = Get-RandomItemFromList $INSTANCEHASH[$UserClass]
                ActivityAssignedTo = Get-RandomItemFromList $INSTANCEHASH[$UserClass]
                Reviewer = @{
                    __CLASS = "System.Reviewer"
                    __OBJECT = @{
                        Comments = Get-LoremIpsum 22
                        Decision = Get-RandomItemFromList $ENUMHASH['DecisionEnum']
                        DecisionDate = $CreatedDate.AddHours(12)
                        ReviewerId = [guid]::NewGuid().ToString()
                        }
                    }
                }| new-scsmobjectprojection -Type $ActivityProjectionType -nocommit -whatif:$whatif -verbose:$verbose

        }
        # CREATE THE PROJECTION HASH TABLE
        $p = @{
            __CLASS = "System.WorkItem.Problem"
            __OBJECT = $Seed
            # Now for the Aliases
            AssignedTo          = Get-RandomItemFromList $INSTANCEHASH[$UserClass]
            CreatedBy           = Get-RandomItemFromList $INSTANCEHASH[$UserClass]
            AffectedConfigItems = $ACIs
            }

        # FINISH THE PROJECTION
        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) }
        # set up for closed and resolved status
        if ( $status -match "Closed" -or $status -match "Resolved" )
        {
            $p.__OBJECT['ResolutionDescription'] = get-lorem 22
            $ResolveDiff          = ($TargetResolutionTime - $CreatedDate).TotalHours
            $TimeVariance         = $RANDOM.Next(-$ResolveDiff,$ResolveDiff)
            $ResolvedDate         = $TargetResolutionTime.AddHours($TimeVariance)
            $p.__OBJECT['ResolvedDate'] = $ResolvedDate
            if ( $status -match "Closed" )
            {
                $p.__OBJECT['ClosedDate'] = $ResolvedDate.AddHours($RANDOM.Next(0,2)*24)

                $p['ResolvedBy']    = $ResolvedBy
                $p['ClosedBy']      = $ClosedBy 
            }
            else
            {
                $p['ResolvedBy'] = $ResolvedBy
            }
        }
        # HAND THE PROJECTION TO THE CMDLET
    $p }  | new-SCSMOBjectProjection -Type $PType -bulk -verbose:$verbose -whatif:$whatif -debug:$debug
}