none
Notification that an incident has been unassigned for 30 minutes, recurring every 30.

    Question

  • I am trying to create a notification that will send an email to a specific email address every 30 minutes that an incident is left unassigned in the Tier 2 support group queue. So far I have this... but it doesn't reference the support group, and I am not sure about if it will pick up things that have been open for 30 minutes without being assigned. I can make the job run every 30 minutes...

    Import-Module SMLets
     $getIncidents = Get-SCSMIncident -Status "Active" | where { $_.AssignedTo -eq $NULL }
     if ($getIncidents.count -gt 0)
     {
     $emailFrom = "edited"
     $emailTo = "edited"
     $subject = "There are $($getIncidents.count) unassigned incidents in queue"
     $body = "There is an unassigned incident in the Network Team Queue. This notification will repeat every 30 minutes until the incident is assigned."
     $smtpServer = "edited"
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)
     $smtp.Send($emailFrom, $emailTo, $subject, $body)
     }
     
    else
     
    {
     
    }

    Any help is appreciated.



    • Edited by AudreyPaige Thursday, March 15, 2012 2:46 PM
    Friday, February 17, 2012 9:25 PM

Answers

  • Here we go.

    The script will do the following things:

    • Find all incidents with Status "Resolved" and TierQueue (Support Group) is empty
    • For each incident in the result set it will get the ResolvedBy user and the CreatedBy user
    • If one of the 3 defined users is/are the ResolvedBy or CreatedB user of an incident it will set the incident Status to "Closed" and the TierQueue (Support Group) to  "Undefinded Queue"

    No warranty! Use the script on your own risk!

    Currently the update function is not active (Line 56: ##$CloseIncident |Set-SCSMObject -PropertyHashtable $Propertyhash). The script will list all incidents that will be updated by the script.

    To get the full function remove the 2 "##" in line 56 -> This will update the incidents in your SCSM.

    import-module smlets -force
    cls
    # Define Users
    $User1 = "Administrator"
    $User2 = "Alt"
    $User3 = "PeterPan"
    #
    # Set Target Support Queue (Displayname)
    $DisplayNSupportGrp = 'Undefined Queue' 
    #
    ########### The magic starts here ###########
    #
    # Get Incident status Resolved 
    $Resolved = Get-SCSMEnumeration IncidentStatusEnum.Resolved$
    # Get Incident status Closed
    $Closed = Get-SCSMEnumeration IncidentStatusEnum.Closed$
    # Get Target Support Queue
    $TargetTierQueue = Get-SCSMEnumeration|?{$_.DisplayName -eq $DisplayNSupportGrp}
    # Get Incident class
    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
    # Get Relationship Incident - ResolvedBy
    $ResolvedByUserObjectRelClass = Get-SCSMRelationshipClass System.WorkItem.TroubleTicketResolvedByUser 
    # Get Relationship Incident - CreatedBy
    $CreatedByUserObjectRelClass = Get-SCSMRelationshipClass System.WorkItemCreatedByUser 
    # Set variable = $NULL
    $IncidentToClose = $NULL
    # Prepare PropertyHash
    $Propertyhash = @{"Status" = $Closed ; "Tierqueue" = $TargetTierQueue}
    # Get all incidents with status = "resolved" + Tier Queue = "empty"
    $Incidents = @(Get-SCSMObject -Class $IncidentClass | where {$_.Status -eq $Resolved -AND $_.Tierqueue -eq $NULL})
    # If Incidents found
    If ($Incidents.count -gt 0) 
    	{ 
    	# Do the following commands for each Incident
    	foreach ($Incident in $Incidents)
    		{
    		# Get CreatedBy User of Incident
    		$CreatedByUser = Get-SCSMRelatedObject -SMObject $Incident -Relationship $CreatedByUserObjectRelClass
    		#$CreatedByUser
    		#Output Incident Id and CreatedByUser (just for a short check)
    		Write-Host $Incident.Id "created by" "" -NoNewline; Write-Host $CreatedByUser.Displayname
    		#Get ResolvedBy User of Incident
    		$ResolvedByUser = Get-SCSMRelatedObject -SMObject $Incident -Relationship $ResolvedByUserObjectRelClass
    		#Output Incident Id and CreatedByUser (just for a short check)
    		Write-Host $Incident.Id "resolved by" "" -NoNewline; Write-Host $ResolvedByUser.DisplayName
    		# Check if the user is one of the defined users
    		If ($CreatedByUser.Displayname -eq $User1 -OR $CreatedByUser.Displayname -eq $User2 -OR $CreatedByUser.Displayname -eq $User3 -OR $ResolvedByUser.Displayname -eq $User1 -OR $ResolvedByUser.Displayname -eq $User2 -OR $ResolvedByUser.Displayname -eq $User3)
    				{
    				# Prepare Output of Incidents that will be closed
    				$IncidentToClose = $IncidentToClose + " " + $Incident.Id
    				# Output of Incidents that will be closed by this script (just for a short check)
    				Write-Host "This Incident(s) will be closed:" $IncidentToClose
    				# Preparation for closing
    				$CloseIncident = $Incident
    				# Close Incidents -> That's the dangerous part if something is wrong in the script ;-)
    				##$CloseIncident |Set-SCSMObject -PropertyHashtable $Propertyhash
    				}
    		}
    	}
    	Else {Write-Host "No Incident met the criteria"}

    Hope this helps.


    Andreas Baumgarten | H&D International Group

    Monday, May 06, 2013 4:09 PM
    Moderator

All replies

  • Hi,

    This should do it; I haven't tested it though.

    $Tier2 = Get-SCSMEnumeration -Name IncidentTierQueuesEnum.Tier2$
    $now = Get-Date
    $getIncidents = Get-SCSMIncident -Status "Active" | where { ($_.AssignedTo -eq $NULL) -and ($_.TierQueue -eq $Tier2) -and ($_.CreatedDate.AddMinutes(30) -lt $now)}

    Regards
    //Anders


    Anders Asp | Lumagate | www.lumagate.com | Sweden | My blog: www.scsm.se

    Friday, February 17, 2012 9:43 PM
    Moderator
  • Maybe this is helpful.

    The script gets all unassigned incidents with status active and Support Group equal to Tier 2.

    You need to modify the 3 variables.

    import-module smlets
    
    $smtphost="mailserver"
    $to="mail@domain.com"
    $from="mail@domain.local"
    function Send-Mail
    {
    param($From,$To,$Subject,$Body)
    $smtp = new-object system.net.mail.smtpClient($smtphost)
    $mail = new-object System.Net.Mail.MailMessage
    $mail.from= $From
    $mail.to.add($To)
    $mail.subject= $Subject
    $mail.body= $Body
    $mail.isbodyhtml=$true
    $smtp.send($mail)
    }
    
    $BeforeDate = (get-date).AddMinutes(-30).ToString("yyy-MM-dd HH:mm:ss")
    $Active = Get-SCSMEnumeration IncidentStatusEnum.Active$
    $TierQueue = (Get-SCSMEnumeration IncidentTierQueuesEnum.Tier2$)
    
    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
    $Incidents = Get-SCSMObject -Class $IncidentClass | where {$_.AssignedTo -eq $NULL  -AND $_.Status -eq $Active -AND $_.Tierqueue -eq $TierQueue} | select ID, Title
    
    if ($Incidents.Count -gt 0) {
    $subject = "Unassigned incidents in Service Manager, total of " + $Incidents.Count
    $body = $Incidents|convertto-html
    Send-Mail $from $to $subject $body
    }

    In the SCSM Authoring Tool you can create a workflow triggered every 3o minutes to start the script.

    Hope this helps.


    Andreas Baumgarten | H&D International Group

    Friday, February 17, 2012 9:44 PM
    Moderator
  • I will try this...

    Should this part say Tier2, or as written?

    AND $_.Tierqueue -eq $TierQueue} |

    Friday, February 17, 2012 9:47 PM
  • Please use the "AND $_.Tierqueue -eq $TierQueue} |" as written.

    To get the right Support Group the variable $Tierqueue will be filled here:

    "$TierQueue = (Get-SCSMEnumeration IncidentTierQueuesEnum.Tier2$)"



    Andreas Baumgarten | H&D International Group

    Friday, February 17, 2012 9:50 PM
    Moderator
  • It looks like i missed the "created before 30 minutes" in my  script. Sorry.

    Just replace this:

    $Incidents = Get-SCSMObject -Class $IncidentClass | where {$_.AssignedTo -eq $NULL  -AND $_.Status -eq $Active -AND $_.Tierqueue -eq $TierQueue} | select ID, Title
    


    with this :

    $Incidents = Get-SCSMObject -Class $IncidentClass | where {$_.AssignedTo -eq $NULL  -AND $_.CreatedDate -lt $BeforeDate -AND $_.Status -eq $Active -AND $_.Tierqueue -eq $TierQueue} | select ID, Title
    



    Andreas Baumgarten | H&D International Group

    Friday, February 17, 2012 10:08 PM
    Moderator
  • Ok, thank you so much!

    I have uploaded and am waiting - fingers crossed!

    Monday, February 20, 2012 2:52 PM
  • I have uploaded it, but it seems to be failing in the status log. Should I recreate it maybe?

    A couple questions...

    Does the from email address have to be the service account for SCSM, or can it be any email address?

    If I have changed the display name of the Tier 2 support, would that make a difference, or would the Tier2 code still work? I believe I only changed it on the View in the Work Items area, but I do have other notifications that reference it like "if support group = Network Team, then..."

    Here is the script portion of my xml:

                    <ScriptBody>
    import-module smlets
    
    $smtphost="edited"
    $to="edited"
    $from="edited"
    function Send-Mail
    {
    param($From,$To,$Subject,$Body)
    $smtp = new-object system.net.mail.smtpClient($smtphost)
    $mail = new-object System.Net.Mail.MailMessage
    $mail.from= $From
    $mail.to.add($To)
    $mail.subject= $Subject
    $mail.body= $Body
    $mail.isbodyhtml=$true
    $smtp.send($mail)
    }
    
    $BeforeDate = (get-date).AddMinutes(-30).ToString("yyy-MM-dd HH:mm:ss")
    $Active = Get-SCSMEnumeration IncidentStatusEnum.Active$
    $TierQueue = (Get-SCSMEnumeration IncidentTierQueuesEnum.Tier2$)
    
    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
    $Incidents = Get-SCSMObject -Class $IncidentClass | where {$_.AssignedTo -eq $NULL  -AND $_.CreatedDate -lt $BeforeDate -AND $_.Status -eq $Active -AND $_.Tierqueue -eq $TierQueue} | select ID, Title
    
    if ($Incidents.Count -gt 0) {
    $subject = "Unassigned incidents in Network Team Queue in Service Manager, total of " + $Incidents.Count
    $body = $Incidents|convertto-html
    Send-Mail $from $to $subject $body
    }</ScriptBody>



    • Edited by AudreyPaige Thursday, March 15, 2012 2:42 PM
    Monday, February 20, 2012 5:33 PM
  • Could you please run the script from a PowerShell. Let'S see if some messages are coming up.

    Also you can put some output variables at the end of the script to see the values of the variables:

    $BeforeDate

    $Active $TierQueue $Incidents



    Andreas Baumgarten | H&D International Group

    Monday, February 20, 2012 6:20 PM
    Moderator
  • I am very new to using powershell (actually have not until I just created this in the SCSM authoring tool). I am trying to figure out how to test it - but so far hittng a snag...

    Can I test it in the Authoring tool?

    • Edited by AudreyPaige Monday, February 20, 2012 7:48 PM
    Monday, February 20, 2012 7:48 PM
  • Just copy the script in a file. Name it <something>.ps1 

    Add the 4 lines at the end of the script and save the script.

    Open a PowerShell. Change the directory to the dir you saved the script in. Call the script and wait for error messages and the output of the 4 variables at the end.


    Andreas Baumgarten | H&D International Group

    Monday, February 20, 2012 8:15 PM
    Moderator
  • I am trying this - I have the script saved as test.ps1 and have added the four lines at the end (after the brackets).

    I can't seem to get the script to run though. I am sure I am doing it wrong... I keep getting the term 'test.ps1' is not recognized as the name of a cmdlet.... etc...

    The above is what I can get using the command prompt.
    • Edited by AudreyPaige Monday, February 20, 2012 8:25 PM
    Monday, February 20, 2012 8:20 PM
  • Microsoft Windows [Version 6.1.7600]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Windows\system32>cd c:\users\user\desktop
    
    c:\Users\user\Desktop>powershell.exe -noexit -executionpolicy bypass "test.ps
    1"
    The term 'test.ps1' is not recognized as the name of a cmdlet, function, script
     file, or operable program. Check the spelling of the name, or if a path was in
    cluded, verify that the path is correct and try again.
    At line:1 char:9
    + test.ps1 <<<<
        + CategoryInfo          : ObjectNotFound: (test.ps1:String) [], CommandNot
       FoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    PS C:\Users\user\Desktop> powershell.exe -noexit -executionpolicy bypass "c:\
    users\user\desktop\test.ps1"
    Import-Module : The specified module 'smlets' was not loaded because no valid m
    odule file was found in any module directory.
    At C:\users\user\desktop\test.ps1:1 char:14
    + import-module <<<<  smlets
        + CategoryInfo          : ResourceUnavailable: (smlets:String) [Import-Mod
       ule], FileNotFoundException
        + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm
       ands.ImportModuleCommand
    
    The term 'Get-SCSMEnumeration' is not recognized as the name of a cmdlet, funct
    ion, script file, or operable program. Check the spelling of the name, or if a
    path was included, verify that the path is correct and try again.
    At C:\users\user\desktop\test.ps1:20 char:30
    + $Active = Get-SCSMEnumeration <<<<  IncidentStatusEnum.Active$
        + CategoryInfo          : ObjectNotFound: (Get-SCSMEnumeration:String) [],
        CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    The term 'Get-SCSMEnumeration' is not recognized as the name of a cmdlet, funct
    ion, script file, or operable program. Check the spelling of the name, or if a
    path was included, verify that the path is correct and try again.
    At C:\users\user\desktop\test.ps1:21 char:34
    + $TierQueue = (Get-SCSMEnumeration <<<<  IncidentTierQueuesEnum.Tier2$)
        + CategoryInfo          : ObjectNotFound: (Get-SCSMEnumeration:String) [],
        CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    FullyQualifiedErrorId : CommandNotFoundException
    
    The term 'Get-SCSMObject' is not recognized as the name of a cmdlet, function,
    script file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At C:\users\user\desktop\test.ps1:24 char:28
    + $Incidents = Get-SCSMObject <<<<  -Class $IncidentClass | where {$_.AssignedT
    o -eq $NULL  -AND $_.CreatedDate -lt $BeforeDate -AND $_.Status -eq $Active -AN
    D $_.Tierqueue -eq $TierQueue} | select ID, Title
    The term 'Get-SCSMClass' is not recognized as the name of a cmdlet, function, s
    cript file, or operable program. Check the spelling of the name, or if a path w
    as included, verify that the path is correct and try again.
    At C:\users\user\desktop\test.ps1:23 char:31
    + $IncidentClass = Get-SCSMClass <<<<  -Name System.WorkItem.Incident$
        + CategoryInfo          : ObjectNotFound: (Get-SCSMClass:String) [], Comma
       ndNotFoundException
        +     + CategoryInfo          : ObjectNotFound: (Get-SCSMObject:String) [], Comm
       andNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    2012-02-20 14:53:55
    PS C:\Users\user\Desktop>
    This is from the command prompt:




    • Edited by AudreyPaige Thursday, March 15, 2012 2:47 PM
    Monday, February 20, 2012 8:24 PM
  • Could you please try this in Powershell:

    .\test.ps1


    Andreas Baumgarten | H&D International Group

    Monday, February 20, 2012 8:25 PM
    Moderator
  • Then I get this:

    File C:\users\user\desktop\test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Pl
    ease see "get-help about_signing" for more details.
    At line:1 char:11
    + .\test.ps1 <<<<
        + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
        + FullyQualifiedErrorId : RuntimeException
    But when I check my executionpolicy - they are all undefined.


    • Edited by AudreyPaige Thursday, March 15, 2012 2:43 PM
    Monday, February 20, 2012 8:27 PM
  • And make sure you have the SMLets installed: http://smlets.codeplex.com

    Andreas Baumgarten | H&D International Group

    Monday, February 20, 2012 8:29 PM
    Moderator
  • Says the installation package is not supported by my processor type.
    Monday, February 20, 2012 8:34 PM
  • Please install the SMLets on the SCSM server and run the script on the SCSM server in a Powershell.

    Before starting the script run the following command in PowerShell:

    Set-ExecutionPolicy unrestricted

     


    Andreas Baumgarten | H&D International Group

    Monday, February 20, 2012 8:37 PM
    Moderator
  • I have to get help from our network team to access the server - it may be tomorrow before they have time to help me. I will get back to you.

    On a side note - they restarted the services on the management server, and now the job is succeeding in SCSM - no email coming through though.

    • Edited by AudreyPaige Monday, February 20, 2012 9:12 PM
    Monday, February 20, 2012 8:57 PM
  • Ok, so for some strange reason at 12 last night, the emails started coming through - weird. They are coming every two hours though it seems like. Is there a setting I should change to make it go every 30 minutes? The job is running every 30 minutes...
    • Edited by AudreyPaige Tuesday, February 21, 2012 1:19 PM
    Tuesday, February 21, 2012 1:18 PM
  • In the script is no timer to trigger the script. 

    How do you trigger the script? Do you created a management pack in Authoring Tool? There you can define the interval. Or do you use the task scheduler? 

     

    Andreas Baumgarten | H&D International Group

    Tuesday, February 21, 2012 1:27 PM
    Moderator
  • I created it in the authoring tool and set it to run every 30 minutes (which I see it doing in the workflow status). It is sending the email every 2 hours though.
    Tuesday, February 21, 2012 1:38 PM
  • That's a little weird.

    But here is how the workflows are handled inside SCSM:

    By default there is a polling interval of 100 seconds the SCSM database will be queried if a condition of a workflow is meet. If the condition is meet the workflow will be added in a queue to be procesed. Depending on the workload in the queue the workflow is processed. Se time taht is needed for this really depends on the lenght of the queue, so it can be done fast or last a little longer on high workload.

    So if the worklow succesfully finished the script finished as well. So maybe it takes a while to send the mail by mail server?


    Andreas Baumgarten | H&D International Group

    Tuesday, February 21, 2012 1:59 PM
    Moderator
  • I don't know - it is coming exactly every two hours, down to the minute. Could it have to do with the wording in the script?
    Tuesday, February 21, 2012 3:00 PM
  • Hmm. I don't think so but you never know ;-)

    Best is to run the script on the SCSM server in a PowerShell. So you can see, how long it takes to finish the script.

    Open a PowerShell from the Start Menu, and start the script (it's still the test.ps1?). That's the best way to see how long it takes from the end of the script until the mail arrived. 

    If you modify this line just for testing and run manually from Powershell:

    $subject = "Unassigned incidents in Service Manager, total of " + $Incidents.Count

    to

    $subject = "Just a Test: Unassigned incidents in Service Manager, total of " + $Incidents.Count

    you can be sure to find the right mail in the mailbox.


    Andreas Baumgarten | H&D International Group

    Tuesday, February 21, 2012 3:10 PM
    Moderator
  • Just an update: running the script on the server worked fine, so I think there is some other problem causing the delay in the timed notifications. Now there is another problem though - ransomly over the weekend the script started pulling everything in the queue nstead of only unassigned. Or rather, now it is seeing all of them as unassigned when they are not. I have turned it all off for now becuase I cannot figure out where the issue is and my network guy says we need to open a ticket with MS. Basically what I was getting was a list of all the network team incidents every 30 mins, then hour and fifteen, then 30, then hour and fifteen.

    I am being told this may be easier to do in the new version of SCSM, or maybe with Opalis. I guess we might just have to wait on it. :(

    Monday, March 05, 2012 9:20 PM
  • Thanks for you feedback.

    Sorry, my fault. I missed the fact the "Assigned To User" is a relationship and the information isn't part of the incident class.

    I added the new script to the Technet Gallery:

    TechNet Gallery > System Center > SCSM: Mail Notification - Unassigned Incidents v0.2 *Updated* 

    Hopefully it is working properly now.


    Andreas Baumgarten | H&D International Group

    Sunday, March 11, 2012 1:26 PM
    Moderator
  • I tried to create this with the authoring tool, but I get an error invalid charachter line 2, position 308 when I try to save.

    Same thing once I change the variables, but it says 309 instead.

    In the SupportGroup= line, do I put in the display name of my Tier2 group? ie Network Team? Or should it say the enum value?

    We also tested the script alone on the server, and this was the error:

    PS E:\temp>     .\UnassignedIncidentsMail-V0.2.ps1
    Get-SCSMRelatedObject : Cannot bind argument to parameter 'SMObject' because it is null.
    At E:\temp\UnassignedIncidentsMail-V0.2.ps1:45 char:49
    +     $AssignedUser = Get-SCSMRelatedObject -SMObject <<<<  $Incident -Relationship $AssignedUserObjectRelClass
        + CategoryInfo          : InvalidData: (:) [Get-SCSMRelatedObject], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,SMLets.GetSMRelatedObjectCommand
    I do get an email, but the group name in the subject and the body are both blank.



    • Edited by AudreyPaige Tuesday, March 13, 2012 2:32 PM
    Tuesday, March 13, 2012 2:08 PM
  • For "Tier 2" the enum criteria is "Tier2". Same for "Tier 3" = "Tier3"

    For the "Network Team" the GUID is needed. Something like "818c5aab22834d419400cb1bc9ac2b79". The Line 32 needs to be modified to: $TierQueue = (Get-SCSMEnumeration $SupportGroup$) to use the GUID as the criteria.

    To get the GUID you can run the following command in a PowerShell:

    Get-SCSMEnumeration|?{$_.DisplayName -eq 'Network Team'}
    ---

    The error in "error invalid charachter line 2, position 308is weird. In my script line 2 is "Import-Module SMlets" ... Position 308?   There are only 21 chars.

    ---

    The error "Get-SCSMRelatedObject : Cannot bind argument to parameter 'SMObject' because it is null." you can ignore. This error message will be raised if no $incidents are found.

    I added 2 lines at line 48 and 72 in the new script (V0.3) to prevent this error message now.


    Andreas Baumgarten | H&D International Group

    Tuesday, March 13, 2012 10:03 PM
    Moderator
  • Regarding the "error invalid charachter line 2, position 308" error.

    The line and position is quite useless in this error message. It seems that the authoring tool is running the whole PS script in a single line or something similar. With that said I bet it's the use of the special citate signs, “ and ” used in several places in the script that are causing the invalid characters in the authoring tool. Try replacing them with regular citate signs ".

    Regards
    //Anders


    Anders Asp | Lumagate | www.lumagate.com | Sweden | My blog: www.scsm.se

    Wednesday, March 14, 2012 8:38 AM
    Moderator
  • I think it may be referring to the xml output file. There is a position 308 on line two of that file.
    Wednesday, March 14, 2012 1:53 PM
  • My Network Team is just the Name of the Tier2 Queue - I just changed the name of the view and the support group in the drop down I guess. So if I just change line 17 to $SupportGroup = "Tier2" is that all I need to change?

    The other error I think may have been referring to the XML file, but I will try it this way and see what I get.

    Wednesday, March 14, 2012 3:14 PM
  • Just give it a try with the "Tier2" in line 17 than.

    Andreas Baumgarten | H&D International Group

    Wednesday, March 14, 2012 10:30 PM
    Moderator
  • It isn't returning an email now. :(
    Thursday, March 15, 2012 1:51 PM
  • What happens if you start the script from a PowerShell prompt? What values you get for $incident.count and $output? 

    What happens if you run the script with Tier1 as queue?

    There are incidents, that are meeting the criteria (created 30 minutes before, in the right tier queue, active) and they are unassigned?


    Andreas Baumgarten | H&D International Group

    Thursday, March 15, 2012 2:50 PM
    Moderator
  • Ok, I tested it now that my incident is older than 30 mins (d'oh!) and it worked on the server using Tier2! :)

    I created it as a workflow in the authoring tool and imported it into SCSM anyway (even though the line 2 error),

    The job fails and gives error code 2130771918, which looks like it has to do with not havign a dll on the server perhaps? There wasn't a dll created when I saved the project in the authoring tool. Is there a dll I need to copy to the server?

    EDIT: I did a find replace on all the " in the script and just replaced them with " again, and now it let me save without an error AND generated a dll! I have to get my dll copied to the server and then I can test it again. Apparently it didn't like the quotation marks that were in there... ???


    • Edited by AudreyPaige Thursday, March 15, 2012 4:44 PM
    Thursday, March 15, 2012 3:41 PM
  • Everything has been loaded for a while now, and the script appears to work but I still have that fishy sporadic email problem. The job fires every 30 minutes and runs successfully except for 7:30an, 8:00am, and 8:30am, when it fails. But, I get emails sporadically through the day. Yesterday I got one at 4pm and 4:30. Today I got 9, 9:30, 10 and 10:30am, but that is it. It is very strange.
    Wednesday, March 21, 2012 4:57 PM
  • Just to be sure: The script will only send an email if at least one incident meets all the criteria (incident is active, support group = xyz, created 30 minutes before an is still unassigned (created date is UTC!)). If no incident meets all the criteria no email is send.

    Do you have the chance to monitor/track the incoming mails via SMTP on the mail server?


    Andreas Baumgarten | H&D International Group

    Wednesday, March 21, 2012 11:53 PM
    Moderator
  • Yes, I have a fake test one I have left open this whole time. In theory it should send an email every 30 minutes for that one at least. Today it has been working since 9am. I continue to monitor.

    I will reach out to my mail admin for help with the monitoring, but today we had some issues that she is probably busy with, so it may be a day or two.

    Thursday, March 22, 2012 6:35 PM
  • Andreas,

    Could you please help?

    Your script is working great.  However, we wanted to modify the script to include a “foreach loop” so that only incidents that returns from the query get process.    The “ForEach loop” is where we are encounting the problem.  The script ran fine, but the status did not get modified.  Here is the script. Thank you.

    $Active = Get-SCSMEnumeration IncidentStatusEnum.Active$

    $TierQueue = (Get-SCSMEnumeration IncidentTierQueuesEnum.Tier2$)

    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$

    $Incidents = Get-SCSMObject -Class $IncidentClass | where {$_.AssignedTo -eq $NULL  -AND $_.Status -eq $Active -AND $_.Tierqueue -eq $TierQueue} | Select ID, Title

    FOREACH ($IncidentID in $Incidents)

    {

    Get-SCSMIncident  -ID  $IncidentID  |  Set-SCSMIncident   -Status   Closed

    }

    Thursday, April 25, 2013 9:34 PM
  • If I got your requirement right you want to close all incidents that are in Support Group ="Tier2" AND Status = "Active" AND "not assigned"?

    If so you can try this script:

    import-module smlets -force
    
    # Get enum "Active"
    $Active = Get-SCSMEnumeration IncidentStatusEnum.Active$
    # Get enum "Closed"
    $Closed = Get-SCSMEnumeration IncidentStatusEnum.Closed$
    # Get Tier2 TierQueue
    $TierQueue = (Get-SCSMEnumeration IncidentTierQueuesEnum.Tier2$)
    # Get Incident class
    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
    # Set property hash
    $Propertyhash = @{"Status" = $Closed}
    # Get all incidents = active + not assigned + Tier2
    $Incidents = @(Get-SCSMObject -Class $IncidentClass | where {$_.AssignedTo -eq $NULL  -AND $_.Status -eq $Active -AND $_.Tierqueue -eq $TierQueue})
    # If incidents are found
    If ($Incidents.Count -gt 0)
    	{
    	# For each incident
    	Foreach ($IncidentToClose in $Incidents)
    		{
    			# Set status of Incident = Closed
    			$IncidentToClose |Set-SCSMObject -PropertyHashtable $Propertyhash
    		}
    	}
    remove-module smlets -force
    Hope this helps.


    Andreas Baumgarten | H&D International Group

    Friday, April 26, 2013 6:11 PM
    Moderator
  • Andreas,

    One Million Thank to you, your script is working perfectly. 

    Could you please provide help on insert a “comment” in the action log as well?

    Again, thank you so much for the effort!

    Monday, April 29, 2013 10:29 PM
  • Maybe this blog post is helpful:

    Creating Membership and Hosting Objects/Relationships Using New-SCSMObjectProjection in SMLets


    Andreas Baumgarten | H&D International Group

    Tuesday, April 30, 2013 5:44 AM
    Moderator
  • Hello,

    I was using “Get-SCSMObject –Class (Get-SCSMClass –Name System.WorkItem.Incident$) | Get-Member” to see which properties are available, but I cannot find the following properties:

    AssignedTo

    ResolvedBy

    ClosedBy

     

    I was trying to see if I can use “Get-SCSMObject” to retrieve incidents. I was able to obtain AssignedTo, but not  ResolvedBy or ClosedBy incidents.

     

    I’m very new at powershell.  Your advice is very much appreciated.

    Friday, May 03, 2013 2:04 PM
  • AssignedTo, ResolvedBy and ClosedBy are relationships between work item and ad user class.

    Just a short code snippet:

    # Define some variables:
    $AssignedUserObjectRelClass = Get-SCSMRelationshipClass System.WorkItemAssignedToUser 
    $IncidentClass = Get-SCSMClass -name System.WorkItem.Incident$ 
    # Get incidents
    $Incidents= @(Get-SCSMObject -Class $IncidentClass | Where {($_...... <add some filter here> ....)
    # Get AssignedTo user of each incident
    If ($Incidents.count -gt 0)  
    {  
      
    #Get all assigned incidents from list  
    foreach ($Incident in $Incidents)  
        {  
        # Get AssignedToUser 
        $AssignedUser = Get-SCSMRelatedObject -SMObject $Incident -Relationship $AssignedUserObjectRelClass
    $AssignedUser
        }
    }

    Hope this helps. 

    Andreas Baumgarten | H&D International Group

    Friday, May 03, 2013 5:22 PM
    Moderator
  • Thank you for the quick reply. It will take me months or years to able to write the code like yours.

    I modified the code to add a filter, but no incidents return.  Any suggestions would be helpful.  Thanks

    $Incidents= @(Get-SCSMObject -Class $IncidentClass | Where {$_ResolvedBy –eq ‘Ngo,Keang’})

    Friday, May 03, 2013 6:29 PM
  • Maybe you can describe what you want to do/get with the script? What should be the output? I can give it a try than. 


    Andreas Baumgarten | H&D International Group

    Friday, May 03, 2013 6:33 PM
    Moderator
  • I was asked to modify some of the data in Service Manager that were done incorrectly.

    The script needs to find all incidents with the following criteria:

    • Status = “Resolved”
    • CreatedBy or ResolvedBy  in (user1, user2, user3)
    • Support Queue = Empty

    Set all incidents:  

    • Status = “Closed”
    • Support Queue = “Undefined Queue”

    This has been a nightmare.  I could not find any powershell script on the web that would help.

    Friday, May 03, 2013 7:48 PM
  • Just to be sure:
    CreatedBy OR ResolvedBy should be User1 OR User2 OR User 3?

    Maybe you like to share some information of the use case? Why do you need this?  


    Andreas Baumgarten | H&D International Group

    Friday, May 03, 2013 9:42 PM
    Moderator
  • Some of our support analysts are still very new at Service Manager. I constantly found incidents that were entered by them were incorrect, such as “Resolved” incidents without a “Support Queue”.

    It is very critical that we can have a script that query all “Resolved” incidents that were “ResolvedBy”  OR “Creatredby” User1, User2, User3, etc…………… and Set the Status = “Closed” And the Support Queue = “Undefined Queue”

    This script will not only help to “Closed” all exiting incidents with the correct information, but it also ensure that correctness data were populated in the system so that we can use such data for reporting purposes.  

    If you could help that would be wonderful.  Thank you!

    Monday, May 06, 2013 2:39 PM
  • Here we go.

    The script will do the following things:

    • Find all incidents with Status "Resolved" and TierQueue (Support Group) is empty
    • For each incident in the result set it will get the ResolvedBy user and the CreatedBy user
    • If one of the 3 defined users is/are the ResolvedBy or CreatedB user of an incident it will set the incident Status to "Closed" and the TierQueue (Support Group) to  "Undefinded Queue"

    No warranty! Use the script on your own risk!

    Currently the update function is not active (Line 56: ##$CloseIncident |Set-SCSMObject -PropertyHashtable $Propertyhash). The script will list all incidents that will be updated by the script.

    To get the full function remove the 2 "##" in line 56 -> This will update the incidents in your SCSM.

    import-module smlets -force
    cls
    # Define Users
    $User1 = "Administrator"
    $User2 = "Alt"
    $User3 = "PeterPan"
    #
    # Set Target Support Queue (Displayname)
    $DisplayNSupportGrp = 'Undefined Queue' 
    #
    ########### The magic starts here ###########
    #
    # Get Incident status Resolved 
    $Resolved = Get-SCSMEnumeration IncidentStatusEnum.Resolved$
    # Get Incident status Closed
    $Closed = Get-SCSMEnumeration IncidentStatusEnum.Closed$
    # Get Target Support Queue
    $TargetTierQueue = Get-SCSMEnumeration|?{$_.DisplayName -eq $DisplayNSupportGrp}
    # Get Incident class
    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
    # Get Relationship Incident - ResolvedBy
    $ResolvedByUserObjectRelClass = Get-SCSMRelationshipClass System.WorkItem.TroubleTicketResolvedByUser 
    # Get Relationship Incident - CreatedBy
    $CreatedByUserObjectRelClass = Get-SCSMRelationshipClass System.WorkItemCreatedByUser 
    # Set variable = $NULL
    $IncidentToClose = $NULL
    # Prepare PropertyHash
    $Propertyhash = @{"Status" = $Closed ; "Tierqueue" = $TargetTierQueue}
    # Get all incidents with status = "resolved" + Tier Queue = "empty"
    $Incidents = @(Get-SCSMObject -Class $IncidentClass | where {$_.Status -eq $Resolved -AND $_.Tierqueue -eq $NULL})
    # If Incidents found
    If ($Incidents.count -gt 0) 
    	{ 
    	# Do the following commands for each Incident
    	foreach ($Incident in $Incidents)
    		{
    		# Get CreatedBy User of Incident
    		$CreatedByUser = Get-SCSMRelatedObject -SMObject $Incident -Relationship $CreatedByUserObjectRelClass
    		#$CreatedByUser
    		#Output Incident Id and CreatedByUser (just for a short check)
    		Write-Host $Incident.Id "created by" "" -NoNewline; Write-Host $CreatedByUser.Displayname
    		#Get ResolvedBy User of Incident
    		$ResolvedByUser = Get-SCSMRelatedObject -SMObject $Incident -Relationship $ResolvedByUserObjectRelClass
    		#Output Incident Id and CreatedByUser (just for a short check)
    		Write-Host $Incident.Id "resolved by" "" -NoNewline; Write-Host $ResolvedByUser.DisplayName
    		# Check if the user is one of the defined users
    		If ($CreatedByUser.Displayname -eq $User1 -OR $CreatedByUser.Displayname -eq $User2 -OR $CreatedByUser.Displayname -eq $User3 -OR $ResolvedByUser.Displayname -eq $User1 -OR $ResolvedByUser.Displayname -eq $User2 -OR $ResolvedByUser.Displayname -eq $User3)
    				{
    				# Prepare Output of Incidents that will be closed
    				$IncidentToClose = $IncidentToClose + " " + $Incident.Id
    				# Output of Incidents that will be closed by this script (just for a short check)
    				Write-Host "This Incident(s) will be closed:" $IncidentToClose
    				# Preparation for closing
    				$CloseIncident = $Incident
    				# Close Incidents -> That's the dangerous part if something is wrong in the script ;-)
    				##$CloseIncident |Set-SCSMObject -PropertyHashtable $Propertyhash
    				}
    		}
    	}
    	Else {Write-Host "No Incident met the criteria"}

    Hope this helps.


    Andreas Baumgarten | H&D International Group

    Monday, May 06, 2013 4:09 PM
    Moderator
  • Andreas,

    You are fantastic!  I can’t thank you enough.

    I have tested the script and it is working exactly the way we wanted. 

    You are not only saving me from spending many hours trying figuring out how to write the script, but you also making a newbie’s life much easier.

    Thank you…………………………………… for your time and effort.  I learned a lot from your script.

    PS – Is there a website where this type of example scripts are posted for newbie’s can visit and obtain knowledge.

    Tuesday, May 07, 2013 4:12 PM
  • You are welcome.

    Some good and helpful SCSM related PowerShell scripts are in the TechNet Gallery (search for SCSM):

    http://gallery.technet.microsoft.com/site/search?query=SCSM&f%5B0%5D.Value=SCSM&f%5B0%5D.Type=SearchText&ac=4


    Andreas Baumgarten | H&D International Group

    Tuesday, May 07, 2013 4:20 PM
    Moderator
  • This isn't really what I was looking for. I think this thread has split off at some point.
    Wednesday, May 08, 2013 8:50 PM