Sending unassigned incident list using powershell script

Pergunta Sending unassigned incident list using powershell script

  • jeudi 5 janvier 2012 14:23
     
      A du code
    Get-SCSMIncident -Status "Active" -CreatedBefore $BeforeDate | where { $_.AssignedTo -eq $NULL -AND $_.SupportGroup.DisplayName -eq "groupname" }
    


    With the above code is there anything wrong? The incidents which are not assigned and has the specific support group selected.

    The whole code block is as follows. It will email all the unassigned incidents and has the specific support group selected. But it doesnt !!!

     

    $smtphost="servername"
    $to="mail@domain.com"
    $from="mail@domain.com"
    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)
    }
    Import-Module SMLets
    $BeforeDate = (get-date).AddMinutes(-10).ToString("yyy-MM-dd HH:mm:ss")
    
    $getIncidents = Get-SCSMIncident -Status "Active" -CreatedBefore $BeforeDate | where { $_.AssignedTo -eq $NULL -AND $_.SupportGroup.DisplayName -eq "groupname" } | select ID, Title, AffectedUser
    
    if ($getIncidents.count -gt 0) {
    
    $subject= "Unassigned incidents in Service Manager, total of " + $getIncidents.count
    
    $body=$getIncidents|convertto-html
    
    Send-Mail $from $to $subject $body
    
    }
    
    Remove-Module SMLets
    

Toutes les réponses

  • mercredi 11 janvier 2012 00:46
    Modérateur
     
      A du code

    Maybe you like to try this:

     

    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(-10).ToString("yyy-MM-dd HH:mm:ss")
    $Active = Get-SCSMEnumeration IncidentStatusEnum.Active$
    $TierQueue = (Get-SCSMEnumeration IncidentTierQueuesEnum.Tier1$)
    
    $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, AffectedUser
    
    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
    }
    
    

     


    Tested with SCSM 2010 SP1 CU and SMlets Beta3.

    In our SCSM 2010 environment it's working:

    Hope this helps.


    Andreas Baumgarten | H&D International Group
  • mercredi 11 janvier 2012 08:26
     
      A du code

    Hi Andreas,

    Thank you for your quick solution. Unfortunatelly it didnt work correctly for me. I have noticed that $BeforeDate is not mentioned within $Incidents. Is the following string correct? Also I have a TierQueue called "CRM". How to define this?

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


     

  • mercredi 11 janvier 2012 12:05
    Modérateur
     
      A du code

    Sorry I forgot the "Before" Part:

     

    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(-10)
    $Active = Get-SCSMEnumeration IncidentStatusEnum.Active$
    $TierQueue = (Get-SCSMEnumeration IncidentTierQueuesEnum.Tier1$)
    
    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
    $Incidents = Get-SCSMObject -Class $IncidentClass | where {$_.AssignedTo -eq $NULL  -AND $_.Status -eq $Active -AND $_.Tierqueue -eq $TierQueue -AND $_.CreatedDate -lt $BeforeDate} | select ID, Title, AffectedUser
    
    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
    }
    
    


    Andreas Baumgarten | H&D International Group
  • mercredi 11 janvier 2012 12:22
     
     
    The script sends email but it says a total of 200 unassigned. However I have currently only 4 unassigned incidents. I think it lists all assigned and unassigned incidents.
  • mercredi 11 janvier 2012 12:25
    Modérateur
     
     
    Here the script works like expected. Just did some test runs.
    Andreas Baumgarten | H&D International Group
  • mercredi 11 janvier 2012 12:28
     
     
    What if I have a TierQueue called "CRM" which is not a default tier queue?
  • mercredi 11 janvier 2012 12:39
     
     
    Also the affected user field comes empty :((
  • mercredi 11 janvier 2012 22:11
    Modérateur
     
     

    I am not able to connect to our SCSM environment at the moment. So i can't try it by myself.

    The affected user is a relationship between incident and users.

    There is a good blog post of Patrik Sundqvist how to get the related affected user of an incident:

    Affected User SMTP Address

    Maybe this helps to get the affected user name of an incident.


    Andreas Baumgarten | H&D International Group