Search

Friday, November 22, 2013

Closing resolved incidents automatically in SCSM with Powershell and/or with SCSM tasks

Hi,

Today, I am going to write down the steps and a powershell script I used for closing resolved incidents automatically in SCSM and/or with SCSM tasks. To note, the environment I work on is a SCSM 2012 SP1 on a 2008 R2 SP1 server.

I had a need for closing incidents after sometime when incidents left resolved but not closed by users. Therefore, we agreed on closing resolved incidents after 2 days automatically with SCSM using a Powershell script running as a scheduled task.

If we explain briefly, what we are going to do is;
  1. For a later use to use as a task on any console importing SMLets module, log file path and name, SM management computer name,
  2. Getting the date script run and getting incidents both resolved and resolved date more than 48 hours,
  3. For each incident filtered closing the incident and inserting the closed date while writing to the log file specified before.
  4. Preparing a scheduled task for automation of the script.
  5. If desired adding as a task on console to run manually when desired.


Import-Module SMLets
$LogFilename = "anypath\yourlogname.log"
$SMDefaultComputer = "SCSM management computer name"

$Date = (get-date)
$DateString = $Date.toString()
$2DaysOld = (get-date).addhours(-48)
$Incidents = @(Get-SCSMObject –Class(get-scsmclass system.workitem.incident$) | where{$_.resolveddate -lt $2DaysOld -and $_.status -like "*resolv*"})

foreach ($Inc in $Incidents)
{Add-Content -Path $LogFilename ($Date.toString() + " - Any description you desire - " + $Inc.ID + " - " + $Inc.Title)
sleep 1
set-SCSMObject –property Status -Value Closed -SMobject $Inc
Set-SCSMObject –property ClosedDate -Value $Date -SMobject $Inc
}
Add-Content -Path $LogFilename (".")



Save your powershell script to anywhere you want, then you can add as a scheduled task into Task Scheduler like below;

Configure and trigger the script for everyday every 1 hour period;

 
In "Program/script" section enter;
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
 
In "Add arguments" section enter;
C:\YourPathWhereScriptSaved\YourScriptFilename.ps1

If you want to Add the script as a task into SCSM console;

On the console go to Library-->Tasks section and create new task. Enter name and description, select "Incident" as target class, choose a custom management pack or create a new one.


Move down to command line tab and fill areas as in screenshot;



Now you will be able to see the task on your consoles Tasks Pane on the right side.


I hope this blog will help you getting over the issue.

C u!..

No comments:

Post a Comment