Scripts/Set-IncidentUser.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
#requires -version 2.0
[CmdletBinding(SupportsShouldProcess=$true)]
param (
    [Parameter()][string]$AssignedTo,
    [Parameter()][string]$Affected,
    [Parameter(ValueFromPipeline=$true)]$TheIncident
)
BEGIN
{
    $NS = "Microsoft.EnterpriseManagement"
    $EMGType = "${NS}.EnterpriseManagementGroup"
    $EMG = new-object ${EMGType} localhost
 
    $ASSIGNEDTORELATIONSHIP = $EMG.EntityTypes.GetRelationshipClasses()|?{$_.name -eq "System.WorkItemAssignedToUser"}
    $AFFECTEDRELATIONSHIP = $EMG.EntityTypes.GetRelationshipClasses()|?{$_.name -eq "System.WorkItemAffectedUser" }
    $DEFAULT = ("${NS}.Common.ObjectQueryOptions" -as "type")::Default
    $EMOT    = "${NS}.Common.EnterpriseManagementObject" -as "type"
    $EMOP = "EnterpriseManagementObjectProjection"
    $IPT = "System.WorkItem.Incident.ProjectionType"
    $CEMO = "${NS}.Common.CreatableEnterpriseManagementObject"
    $INCIDENTC = $EMG.EntityTypes.GetClasses() |?{$_.name -eq "System.WorkItem.Incident" }
    $COMMENTC = $EMG.EntityTypes.GetClasses()|?{$_.name -eq "System.WorkItem.TroubleTicket.AnalystCommentLog"}
    $USERC    = $EMG.EntityTypes.GetClasses()|?{$_.name -eq "System.User"}
    $PROJECTION = $EMG.EntityTypes.GetTypeProjections()| ?{$_.name -eq $IPT}
    $MPCLASSTYPE = "${NS}.Configuration.ManagementPackClass"
    $SYSTEMMP = $EMG.ManagementPacks.GetManagementPacks()|?{$_.name -eq "System.Library"}

    function Get-User
    {
 
        param ( 
            [parameter(Mandatory=$true,Position=0)][string]$displayname
            )
            $criteriaString = '
     <Criteria xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/">
      <Reference Id="System.Library" Version="{0}" PublicKeyToken="{1}" Alias="System" />
      <Expression>
        <SimpleExpression>
          <ValueExpressionLeft><Property>$Target/Property[Type=''System!System.Domain.User'']/DisplayName$</Property></ValueExpressionLeft>
          <Operator>Equal</Operator>
          <ValueExpressionRight><Value>{2}</Value></ValueExpressionRight>
        </SimpleExpression>
      </Expression>
    </Criteria>
    '
 -f $SYSTEMMP.Version, $SYSTEMMP.KeyToken, $displayname
        $userclass = get-scsmclass System.Domain.User
        $CriteriaType = "Microsoft.EnterpriseManagement.Common.EnterpriseManagementObjectCriteria"
        $global:criteria = new-object $CriteriaType $criteriastring,$userclass,$EMG
        # write-host -for cyan $criteria
        $r = Get-SCSMObject -criteria $criteria 
# $r = Get-SCSMObject System.Domain.User -filter "DisplayName -eq '$displayName'"
        # write-host -for cyan $r
        $r

    }
    }
    PROCESS
    {
        $incident = $TheIncident.__base

        if ( $AssignedTo )
        {
            $AssignedToUser = Get-User $AssignedTo
            $Incident.Add(($AssignedToUser -as $EMOT) ,$ASSIGNEDTORELATIONSHIP.Target)
            if ( $Incident.Item("AssignedWorkItem").Count -gt 1 )
            {
                $Incident.Item("AssignedWorkItem")[0].Remove()
            }
        }

        if ( $Affected )
        {
            $AffectedUser = Get-User $Affected
            $Incident.Add(($AffectedUser -as $EMOT) ,$AFFECTEDRELATIONSHIP.Target)
            if ( $Incident.Item("CreatedWorkItem").Count -gt 1 )
            {
                $Incident.Item("CreatedWorkItem")[0].Remove()
            }
        }

        if ( $PSCmdlet.ShouldProcess($TheIncident.DisplayName))
        {
            $Incident.Commit()
        }

    }