locked
Using powershell to create an incident RRS feed

  • Question

  • Hi,

    I've created a script based on Andreas Baumgarten notes to create new incidents. It works fine.

    In SCSM we have configured some workflows. One of these Workflows changes the TierQueue depending on the location of the affected user. Also we have configured an announcement to send a mail to the affected user when a new incident was created.

    Unfortunally the workflows and announcements in scsm doesn't take effect on incidents which where created with the powershell script. What am I doing wrong?

    Notes:

    I use Service Manager 2016 Version 7.5.7487.0

    $irClass = Get-SCSMclass -name system.WorkItem.Incident$
    $userClass = Get-SCSMClass -name System.Domain.User$
    
    #Beziehung für Betroffener Benutzer vorbereiten
    $relAffectedUser = Get-SCSMRelationshipClass -Name System.WorkItemAffectedUser$
    $affectedUserObj = Get-SCSMObject -Class $userClass -Filter "Username -eq $affectedUserName"
    
    #Vorfall erzeugen
    $newIR = New-SCSMObject -Class $irClass -PropertyHashtable $propertyhash -PassThru
    
    #Betroffener Benutzer über die eingerichtete Beziehung setzen
    New-SCSMRelationshipObject -Relationship $relAffectedUser -Source $newIR -Target $affectedUserObj -Bulk 


    Thursday, May 9, 2019 12:25 PM

Answers

  • My guess is:

    The Incident object is created in the first step without the affected user by PowerShell ... so there is no Affected User at the time the Incident is created -> The Workflow is not triggered/does not work because there is no Affected User at this time

    In the second step the relationship object "WorkItemAffectedUser" is created ... which counts as an update on the new created Incident.

    Which condition is configured to trigger the workflow "modify TierQueue" and "send notification to affected user"? "On create" or "On Update"?


    Andreas Baumgarten

    • Marked as answer by Ricky Schwabe Monday, May 13, 2019 12:02 PM
    Thursday, May 9, 2019 8:31 PM
  • Maybe using the "Set-SCSMObjectTemplate" cmdlets is working better/without error to apply the template?

    Hope this helps.


    Andreas Baumgarten

    • Marked as answer by Ricky Schwabe Wednesday, May 15, 2019 1:00 PM
    Monday, May 13, 2019 5:10 PM
  • Not that I am aware of. The "Set-SCSMObjectTemplate" cmdlet is pretty much straight forward.

    Andreas Baumgarten

    Tuesday, May 14, 2019 3:34 PM

All replies

  • My guess is:

    The Incident object is created in the first step without the affected user by PowerShell ... so there is no Affected User at the time the Incident is created -> The Workflow is not triggered/does not work because there is no Affected User at this time

    In the second step the relationship object "WorkItemAffectedUser" is created ... which counts as an update on the new created Incident.

    Which condition is configured to trigger the workflow "modify TierQueue" and "send notification to affected user"? "On create" or "On Update"?


    Andreas Baumgarten

    • Marked as answer by Ricky Schwabe Monday, May 13, 2019 12:02 PM
    Thursday, May 9, 2019 8:31 PM
  • In addition to my post before ...

    Maybe this is helpful for you: Workflow that Triggers on Relationship Add or Remove


    Andreas Baumgarten

    Thursday, May 9, 2019 8:47 PM
  • Your Note was very useful. As a result I found the Post "PowerShell Create Incident with AffectedUser Relationship at creation". My new code works fine.

    Import-Module smlets
    
    $smdefaultcomputer = "mySCSM_Server"
    $affectedUserName = "xxx"
    
    
    #Beziehung für Betroffener Benutzer vorbereiten
    $userClass = Get-SCSMClass -name System.Domain.User$
    $relAffectedUser = Get-SCSMRelationshipClass -Name System.WorkItemAffectedUser$
    $affectedUserObj = Get-SCSMObject -Class $userClass -Filter "Username -eq $affectedUserName"
    
    
    #Vorfall-Eigenschaften definieren
    
    $newIncProps= @{__CLASS = "System.WorkItem.Incident";
    
                    __OBJECT= @{"Id"= "V-{0}";
                                "Title" = "Test Ricky - Bitte nicht auflösen";
                                "Description"= "Test Text";
                                "Status" = "Active";
                                "Classification" = "Störungen";
                                "Urgency" = "Low";
                                "Impact" = "Low";
                                "Source" = "Telefon";
                                "TierQueue" = “IKTNS vorOrt (Bonn)”;
                                }
                                AffectedUser =  $affectedUserObj
                                }
    
    
    
    #Vorfall basierend auf Eigenschaften erstellen
    
    $aNewInc= (New-SCSMObjectProjection -type System.WorkItem.Incident -projection $newIncProps -computername $smdefaultcomputer -passThru).object
    
    $aNewInc.DisplayName

    In the referred post is mentioned that I could use a template to create the incident with the command New-SCSMObjectProjection, but if I try I get the powershell-message, that the element where bound in another relationship ("Ein Ermittlungsdatenelement wurde zurückgewiesen, weil das Element bereits an eine andere Mitgliedschaftsbeziehung gebunden ist.").

    Could you help me once again?

    #Vorfall basierend auf Eigenschaften erstellen
    
    $incidentTemplate = "Arbeitsplatzausstattung / Bereitstellung (genehmigungspflichtig)"
    $templateObj = Get-SCSMObjectTemplate | ? {$_.DisplayName -eq $incidentTemplate}
    
    
    $aNewInc= (New-SCSMObjectProjection -type System.WorkItem.Incident -projection $newIncProps -template $templateObj -computername $smdefaultcomputer -passThru).object



    Monday, May 13, 2019 10:46 AM
  • Maybe using the "Set-SCSMObjectTemplate" cmdlets is working better/without error to apply the template?

    Hope this helps.


    Andreas Baumgarten

    • Marked as answer by Ricky Schwabe Wednesday, May 15, 2019 1:00 PM
    Monday, May 13, 2019 5:10 PM
  • Thanks so far, I will test "Set-SCSMObjectTemplate". Is there something special to consider?
    Tuesday, May 14, 2019 12:09 PM
  • Not that I am aware of. The "Set-SCSMObjectTemplate" cmdlet is pretty much straight forward.

    Andreas Baumgarten

    Tuesday, May 14, 2019 3:34 PM
  • Hi Andreas,

    everything works now. I solved it like I discribed above. There were two things that went wrong.

    1. The displayname of a template must not have a slash, but my templates had slashes.

    2. The used relationships of the projection and the template must be equal. Two of my templates sets values to ResolutionCategory, which ist related with an additional class.

    Import-Module smlets
    
    $smdefaultcomputer = "mySCSM_Server"
    
    $affectedUserName = "xxx"
    $userClass = Get-SCSMClass -name System.Domain.User$
    
    #Beziehung für Betroffener Benutzer vorbereiten
    $relAffectedUser = Get-SCSMRelationshipClass -Name System.WorkItemAffectedUser$
    $affectedUserObj = Get-SCSMObject -Class $userClass -Filter "Username -eq $affectedUserName"
    
    
    #Vorfall-Eigenschaften definieren
    
    $newIncProps= @{__CLASS = "System.WorkItem.Incident";
    
                    __OBJECT= @{"Id"= "V-{0}";
                                "Title" = "Test - Bitte nicht auflösen";
                                "Description"= "Test Text";
                                "Status" = "Active";
                                "Classification" = "Störungen";
                                "Urgency" = "Low";
                                "Impact" = "Low";
                                "Source" = "Telefon";
                                "TierQueue" = “IKTNS vorOrt (Bonn)”;
                                }
                                AffectedUser =  $affectedUserObj
                                }
    
    
    #Vorlagenobjekt erstellen
    $incidentTemplate = "Meine Vorlage"
    $templateObj = Get-SCSMObjectTemplate | ? {$_.DisplayName -eq $incidentTemplate}
    
    
    #Vorfall basierend auf Eigenschaften und Vorlage erstellen (bei doppelter Befüllung einer Eigenschaft wird der in der Vorlage gesetzte Wert gesetzt)
    
    $aNewInc= (New-SCSMObjectProjection -type System.WorkItem.Incident -projection $newIncProps -template $templateObj -computername $smdefaultcomputer -passThru).object
    
    
    $aNewInc.DisplayName

    The path you suggested works well, too. But then the code would be longer by one line.

    #Set Template
    Set-SCSMObjectTemplate -Projection $aNewInc -Template $templateObj
    Thank you for providing me with answers. That really helped me a lot.

    Wednesday, May 15, 2019 12:58 PM