SMLetsExt.psm1

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

#region ReviewActivities
Function Get-SCSMActivityReviewer
{
<#
    .SYNOPSIS
    This function gets the Review users from a review acitivity in SCSM.
      
    .DESCRIPTION
     A function gets the Review users from a review activity in SCSM. The object used must be from the get-scsmobject in the smlets module (the -Useradd variable is the user object from SCSM).
    
     .PARAMETER ID

     .PARAMETER ReviewActivity

    .EXAMPLE
    
     Get-SCSMActivityReviewer -ID RA1234

     .EXAMPLE
     
     $RA = Get-SCSMObject -class (Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$) -Filter "ID -eq RA125"
     
     Get-SCSMActivityReviewer -ReviewActivity $RA

#>

[CmdletBinding(DefaultParameterSetName="string")]
[OutputType([PSObject])]
param(
    
    [Parameter(Mandatory=$true,ParameterSetName="string")]
    [string]$ID,
    [Parameter(Mandatory=$true,ParameterSetName="object")]
    [PSObject]$ReviewActivity 
)

    Begin
    {
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Started ***"
        #Create Collection to return
        $Users = @()

        $RAC = Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$
        $ReviewerRelationship = Get-SCSMRelationshipClass -Name System.ReviewActivityHasReviewer$
        $ReviewerIsUser = Get-SCSMRelationshipClass -Name System.ReviewerIsUser$
    }
    process
    {
        try
        {
            if($PSCmdlet.ParameterSetName -eq "string")
            {
                $ReviewActivity = Get-SCSMObject -Class $RAC -Filter "ID -eq $ID"
            }
            $Reviewers = Get-SCSMRelatedObject -SMObject $ReviewActivity -Relationship $ReviewerRelationship
            foreach($Reviewer in $Reviewers)
            {
                $user = Get-SCSMRelatedObject -SMObject $Reviewer -Relationship $ReviewerIsUser
                $Users += $user
            }
        }
        catch
        {
               Write-Error $PSCmdlet.ThrowTerminatingError($_)
        }
    }
    End
    {
        if($Users.count -gt 0)
        {
            Write-Output $Users
        }
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Finished ***"
    }

}


Function Add-SCSMActivityReviewer
{
    <#
    .SYNOPSIS
    This function adds a user to a review acitivity in SCSM.
      
    .DESCRIPTION
     A function to add a user to a review activity in SCSM. The object used must be from the get-scsmobject in the smlets module (the -Useradd variable is the user object from SCSM).
      
    .EXAMPLE
    
     Add-SCSMActivityReviewer -Useradd $SCSMUser -RActivity $RAObject

     .EXAMPLE
     Add-SCSMActivityReviewer -Username rplank -ActivityID RA123

     .EXAMPLE
     Add-SCSMActivityReviewer -username rplank -ActivityID RA1234 -domain <DomainNetbiosName>
#>

[CmdletBinding(DefaultParameterSetName="string")]
    Param(
        [Parameter(Mandatory=$true,ParameterSetName="string")]
        [string]$ActivityID,
        [Parameter(Mandatory=$true,ParameterSetName="string")]
        [string]$username,
        [Parameter(Mandatory=$false,ParameterSetName="string")]
        [string]$domain,
        [Parameter(Mandatory=$True,ParameterSetName="Object")]
        [PSObject[]]$Useadd,
        [Parameter(Mandatory=$True,ParameterSetName="Object")]
        [PSObject]$RActivity    
    )
    begin
    {
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Started ***"
        $relClassReviewerIsUser = Get-SCSMRelationshipClass System.ReviewerIsUser$
        $relClassReviewActivityHasReviewer = Get-SCSMRelationshipClass System.ReviewActivityHasReviewer$
        $Reviewers = @()
        $ReviewActivityClass = Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$

        

    }
    
    Process
    {
        try
        {
        
            if($PSCmdlet.ParameterSetName -eq "Object")
            {
                # Build a Collection of current Reviewers
                $ReviewerList = Get-SCSMRelatedObject -SMObject $RActivity  -relationship $relClassReviewActivityHasReviewer 
                foreach($Reviewer in $ReviewerList)
                {
                    $Review = Get-SCSMRelatedObject -SMObject $Reviewer -Relationship $relClassReviewerIsUser
                    $Reviewers += $Review
                }
                foreach ($User in $Useadd)
                {
                    if($Reviewers -notcontains $user)
                    {
                        $reviewerClass = get-scsmclass System.Reviewer
                        $reviewerArgs = @{ ReviewerId = "Reviewer{0}" } 
                        $reviewer = new-scsmobject -class $reviewerClass -PropertyHashtable $reviewerArgs -nocommit
                        $rel01 = new-scsmrelationshipobject -nocommit  -Relationship $relClassReviewActivityHasReviewer -sou $RActivity -tar $reviewer
                        $rel02 = new-scsmrelationshipobject -nocommit  -rel $relClassReviewerIsUser -Sour $reviewer -targ $User
                        $rel01.commit()
                        $rel02.commit()
                    }
                }
            }
            if($PSCmdlet.ParameterSetName -eq "string")
            {
                Write-Verbose "Get Review Activity from Id"
                $RActivity = Get-SCSMObject -Class $ReviewActivityClass -Filter "ID -eq $ActivityID"
                Write-Verbose "Build a Collection of current Reviewers"
                $ReviewerList = Get-SCSMRelatedObject -SMObject $RActivity  -relationship $relClassReviewActivityHasReviewer 
                foreach($Reviewer in $ReviewerList)
                {
                    $Review = Get-SCSMRelatedObject -SMObject $Reviewer -Relationship $relClassReviewerIsUser
                    $Reviewers += $Review
                }
                $user = Get-SCSMuser -username $username 
                if($domain -ne $null)
                {
                    if($user.Count -gt 1)
                    {
                        foreach($u in $user)
                        {
                            if($u.Domain -eq $domain)
                            {
                                $user = $u
                                
                            }
                        }
                    }
                }
                if($Reviewers -notcontains $user)
                {
                    $reviewerClass = get-scsmclass System.Reviewer
                    $reviewerArgs = @{ ReviewerId = "Reviewer{0}" } 
                    $reviewer = new-scsmobject -class $reviewerClass -PropertyHashtable $reviewerArgs -nocommit
                    $rel01 = new-scsmrelationshipobject -nocommit  -Relationship $relClassReviewActivityHasReviewer -sou $RActivity -tar $reviewer
                    $rel02 = new-scsmrelationshipobject -nocommit  -rel $relClassReviewerIsUser -Sour $reviewer -targ $User
                    $rel01.commit()
                    $rel02.commit()
                }
            }
        }
        catch
        {
            Write-Error $PSCmdlet.ThrowTerminatingError($_)
        }
    }
    end
    {       
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Finished ***"
    }
 }

Function Remove-SCSMActivityReviewer
 {
 <#
    .SYNOPSIS
    This function removes a user from a review acitivity in SCSM.
      
    .DESCRIPTION
     A function to remove a user from a review activity in SCSM. The object used must be from the get-scsmobject in the smlets module (the -Useradd variable is the user object from SCSM).
     
     .PARAMETER ActivityID

     .PARAMETER UserName

     .PARAMETER Activity

     .PARAMETER User
      
    .EXAMPLE
    
     Remove-SCSMActivityReviewer -ActivityID RA1234 -username userA

     .EXAMPLE
     $user = Get-SCSMObject -class (Get-SCSMClass -name System.domain.User$) -Filter "username -eq <username>"
     $RA = Get-SCSMObject -class (Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$) -Filter "ID -eq RA125"
     
     Remove-SCSMActivityReviewer -Activity $RA -username $user

#>


    [CmdletBinding(DefaultParameterSetName="string")]
    Param(
        [Parameter(Mandatory=$true,ParameterSetName="string")]
        [string]$ActivityID,
        [Parameter(Mandatory=$true,ParameterSetName="string")]
        [string]$username,
        [Parameter(Mandatory=$true,ParameterSetName="object")]
        [PSObject]$Activity,
        [Parameter(Mandatory=$true,ParameterSetName="object")]
        [PSObject]$user

    )
    begin
    {
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Started ***"
        try
        {
            $ReviewActivityClass = Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$
            $ReviewerIsUser = Get-SCSMRelationshipClass System.ReviewerIsUser$
            $ReviewerRelationship = Get-SCSMRelationshipClass -Name System.ReviewActivityHasReviewer$
        }
        catch
        {
            Write-Error $PSCmdlet.ThrowTerminatingError($_)
        }
    }
    process
    {
        try
        {
            if($PSCmdlet.ParameterSetName -eq "string")
            {
                $ReviewActivity = Get-SCSMObject -Class $ReviewActivityClass -Filter "ID -eq $ActivityID"
            }
            $Reviewers = Get-SCSMRelatedObject -SMObject $ReviewActivity -Relationship $ReviewerRelationship

            foreach($Reviewer in $Reviewers)
            {
                $userobj = Get-SCSMRelatedObject -SMObject $Reviewer -Relationship $ReviewerIsUser
                if($PScmdlet.ParameterSetName -eq "string")
                {
                    if($userobj.username -eq $username)
                    {
                        $ReviewertoUserRelationship =(Get-SCSMRelationshipObject -TargetObject $userobj -TargetRelationship $ReviewerIsUser) | where {$_.sourceobject.ID -eq $Reviewer.ID}
                        $ActivitytoReviewerRelationship = Get-SCSMRelationshipObject -TargetObject $ReviewertoUserRelationship.SourceObject -TargetRelationship $ReviewerRelationship
                        $ActivitytoReviewerRelationship | Remove-SCSMRelationshipObject
                        $ReviewertoUserRelationship | Remove-SCSMRelationshipObject 
                    }
                }
                Elseif($PSCmdlet.ParameterSetName -eq "object")
                {
                    if($user.username -eq $userobj.username)
                    {
                        $ReviewertoUserRelationship = $ReviewertoUserRelationship = (Get-SCSMRelationshipObject -TargetObject $userobj -TargetRelationship $ReviewerIsUser) | where {$_.sourceobject.ID -eq $Reviewer.ID}
                        $ActivitytoReviewerRelationship = Get-SCSMRelationshipObject -TargetObject $ReviewertoUserRelationship.SourceObject -TargetRelationship $ReviewerRelationship
                        $ActivitytoReviewerRelationship | Remove-SCSMRelationshipObject
                        $ReviewertoUserRelationship | Remove-SCSMRelationshipObject 
                    }
                }
            }
        }
        catch
        {
            Write-Error $PSCmdlet.ThrowTerminatingError($_)
        }

    }
    End
    {
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Finished ***"
    }
 }


Function Update-SCSMActivityReviewer
{
 <#
    .SYNOPSIS
    This function replaces a Reviewer of a review acitivity in SCSM.
      
    .DESCRIPTION
     A function replaces a Reviewer of a review activity in SCSM. The object used must be from the get-scsmobject in the smlets module (the -Useradd variable is the user object from SCSM).

    .PARAMETER ExistingReviewer

    .PARAMETER NewReviewer

    .PARAMETER ActivityID

    .PARAMETER InProgress

    .PARAMETER Pending
           
    .EXAMPLE
     
     Bulk update all Review Activites in Progess
     Update-SCSMActivityReviewer -ExistingReviewer <username> -NewReviewer <userName> -Inprogress

    .EXAMPLE
    
     Bulk Update all Review Activities in Pending status
     Update-SCSMActivityReviewer -ExistingReviewer <username> -NewReviewer <userName> -Pending
     
    .EXAMPLE
    
     To Update a single Activity
     Update-SCSMActivityReviewer -ExistingReviewer <username> -NewReviewer <userName> -ActivityID RA123
#>

    [cmdletbinding(DefaultParameterSetName="Single")]
    Param(
        [Parameter(Mandatory=$true,ParameterSetName="Single")]
        [string]$ExistingReviewer,
        [Parameter(Mandatory=$true,ParameterSetName="Single")]
        [string]$NewReviewer,
        [Parameter(Mandatory=$True,ParameterSetName="Single")]
        [string]$ActivityID,
        [Parameter(Mandatory=$false,ParameterSetName="Bulk")]
        [switch]$InProgress,
        [Parameter(Mandatory=$false,ParameterSetName="Bulk")]
        [switch]$Pending
    )
    begin
    {
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Started ***"
        $ReviewActivityClass = Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$
        $MatchedRAs = @()
    }
    Process
    {
        try
        {
            if($PSCmdlet.ParameterSetName -eq "Bulk")
            {
                if($InProgress -eq $true)
                {
                    $InProgressID = (Get-SCSMEnumeration -name activitystatusenum.active$).ID
                    $ReviewActivities = Get-SCSMobject -class $ReviewActivityClass -Filter "Status -eq $InProgressID"

                }
                if($Pending -eq $true)
                {
                    $PendingID = (get-SCSMEnumeration -name  ActivityStatusEnum.Ready).ID
                    $ReviewActivities = Get-SCSMobject -class $ReviewActivityClass -Filter "Status -eq $PendingID"
            
                }
                if($InProgress -eq $false -and $Pending -eq $false)
                {
                    Write-Error "Select a switch for Bulk editing"
                }
            }
            else
            {
                $ReviewActivities = Get-SCSMobject -class $ReviewActivityClass -Filter "ID -eq $ActivityID"
            }
            foreach($RA in $ReviewActivities)
            {
                $Reviewers = Get-SCSMActivityReviewer -ReviewActivity $RA
                foreach($Reviewer in $Reviewers)
                {
                    if($Reviewer.username -eq $ExistingReviewer)
                    {
                        $MatchedRAs += $RA.ID
                    }
                }
            }
            if($MatchedRAs.count -gt 0)
            {
                Foreach($RA in $MatchedRAs)
                {
                    Remove-SCSMActivityReviewer -ActivityID $RA -username $ExistingReviewer
                    Add-SCSMActivityReviewer -ActivityID $RA -username $NewReviewer
                }
            }
            else
            {
                Write-output "No Review Activities Matched your Criteria"
            }
        }
        catch
        {
            Write-Error $PSCmdlet.ThrowTerminatingError($_)
        }
    }
    end
    {
        Write-Verbose "*** $($MyInvocation.MyCommand.CommandType) $($MyInvocation.MyCommand.Name) Finished ***"
    }

}
 #endregion