Script check ServiceRequest for Assigned User

Roman_NepomniashchiiRoman_Nepomniashchii Customer IT Monkey ✭
Good afternoon, who can help? I need a script that checks if in the ServiceRequest has a assigned user then need to change the status of the this ServiceRequest. I will be grateful to everyone.
Tagged:

Answers

  • Adam_DzyackyAdam_Dzyacky Customer Contributor Monkey ✭✭✭✭✭
    edited July 25
    This should get you started in the right direction

    $mgmtServer = "localhost"
    $srID = "SR1234"

    $srClass = get-scsmclass -name "system.workitem.servicerequest$" -computername $mgmtServer
    $assignedUserRelClass = get-scsmrelationshipclass -name "System.WorkItemAssignedToUser" -ComputerName $mgmtServer
    $sr = get-scsmobject -class $srClass -filter "Name -eq '$srID'" -computername $mgmtServer
    $assignedUser = get-scsmrelationshipobject -bysource $sr -computername $mgmtServer | ?{($_.relationshipid -eq $assignedUserRelClass.id) -and ($_.sourceobject.classname -eq $srClass.name)}

    if (!$assignedUser)
    {
    Set-SCSMObject -SMObject $sr -Property Status -Value "ServiceRequestStatusEnum.Canceled" -computername $mgmtServer
    }
  • Roman_NepomniashchiiRoman_Nepomniashchii Customer IT Monkey ✭
    This should get you started in the right direction

    $mgmtServer = "localhost"
    $srID = "SR1234"

    $srClass = get-scsmclass -name "system.workitem.servicerequest$" -computername $mgmtServer
    $assignedUserRelClass = get-scsmrelationshipclass -name "System.WorkItemAssignedToUser" -ComputerName $mgmtServer
    $sr = get-scsmobject -class $srClass -filter "Name -eq '$srID'" -computername $mgmtServer
    $assignedUser = get-scsmrelationshipobject -bysource $sr -computername $mgmtServer | ?{($_.relationshipid -eq $assignedUserRelClass.id) -and ($_.sourceobject.classname -eq $srClass.name)}

    if (!$assignedUser)
    {
    Set-SCSMObject -SMObject $sr -Property Status -Value "ServiceRequestStatusEnum.Canceled" -computername $mgmtServer
    }
    Thank you, Adam_Dzyacky, I'll try to figure it out
  • Roman_NepomniashchiiRoman_Nepomniashchii Customer IT Monkey ✭
    This should get you started in the right direction

    $mgmtServer = "localhost"
    $srID = "SR1234"

    $srClass = get-scsmclass -name "system.workitem.servicerequest$" -computername $mgmtServer
    $assignedUserRelClass = get-scsmrelationshipclass -name "System.WorkItemAssignedToUser" -ComputerName $mgmtServer
    $sr = get-scsmobject -class $srClass -filter "Name -eq '$srID'" -computername $mgmtServer
    $assignedUser = get-scsmrelationshipobject -bysource $sr -computername $mgmtServer | ?{($_.relationshipid -eq $assignedUserRelClass.id) -and ($_.sourceobject.classname -eq $srClass.name)}

    if (!$assignedUser)
    {
    Set-SCSMObject -SMObject $sr -Property Status -Value "ServiceRequestStatusEnum.Canceled" -computername $mgmtServer
    }
    I have slightly redesigned this script for my needs and it works. But there is a problem. This script will be used as a workflow, so you need to loop it, it applies to one request $srID = "SR1234", how can you make it apply to requests containing a name such as SR?
  • Adam_DzyackyAdam_Dzyacky Customer Contributor Monkey ✭✭✭✭✭
    edited July 27
    So the good news is that if you're making it a workflow (i.e. a workflow inside of a management pack through the SCSM Authoring tool) you can actually target the Service Request class, then map the ID property of an SR to said input. Here's a screenshot of a workflow I've built around the Incident class:





    I have PowerShell script that has a empty variable assigned within it (so per my above PS script example, instead of hard defining a SR1234 you'd just make it another variable), within the Script Properties tab here the Authoring Tool allows me to map the ID property of an Incident into that empty variable within my script. The workflow runs every hour, looks for Incidents matching some criteria, and then feeds the ID property (i.e. IRxxxx) into the script.

    The bigger decision is does this workflow of yours run on a condition (i.e. something has changed on an SR) or does it run on a schedule (i.e. once an hour). The point is, this is the condition that enables a "looping" of sorts. The script itself doesn't feature a loop, it would just run once foreach SR that meets X criteria.
Sign In or Register to comment.