Powershell Script to populate fields between related objects

Mar 31, 2011 at 5:09 PM
Edited Mar 31, 2011 at 7:06 PM

Hi all

I am relatively new to Powershell, but have been involved heavily in the past few months with Service Manager automation and scripting. I'm a first time poster, but long time fan of this site.

I am trying to use a powershell script to populate the Priority and ScheduledEndDate fields on an activity with the values that exist in the Priority and TargetResolutionTime on the parent incident object. This is the script I have so far. This runs succesfully, but asks me for a parameter SMObject. I don't want to enter a parameter, i just want it to automatically go through and do this for each incident. Any suggestions?

$incident = Get-SCSMClass Incident$
$activityRel = Get-SCSMRelationshipClass sactivity
 
Foreach ($incident in Get-SCSMObject $incident)
{
  $activity = Get-SCSMRelatedObject -Relationship $activityRel|Where{$_.Type -eq 'System.WorkItem.Activity.ManualActivity'}
  if($activity -NE $NULL)
  {
  Set-SCSMObject -Object $activity -Property Priority -Value $incident.Priority
  Set-SCSMObject -Object $activity -Property ScheduledEndDate -Value $incident.TargetResolutionTime
  }
}
Developer
Apr 1, 2011 at 12:03 AM

the Get-SCSMRelatedObject cmdlet is the one that is asking for the smobject because Get-SMRelatedObject works against an instance, so it must be provided.

Try something along the following lines instead (I haven't tried this, but I think it's the right approach):

$incident = Get-SCSMClass System.WorkItem.Incident$
$activityRel = Get-SCSMRelationshipClass sactivity
get-scsmobject $incident | %{
  $_ |get-scsmrelatedobject -relationship $activityRel |
    ?{ $_.type -eq 'System.WorkItem.Activity.ManualActivity' }
  } | set-scsmobject -whatif -propertyHash @{
       AffectedUser = $incidentObject.AffectedUser
       ScheduledEndDate = $incidentObject.TargetResolutionTime
       }
Apr 4, 2011 at 3:37 PM

Thanks Jim

With a good deal of trial and error I was able to get this script to run succesfully for all incidents. I wasn't able to get the Get-SCSMobject line in the code above to work without error, and again apologies as I am fairly novice with Powershell. Here is what I could get to work, do you think this is a good way to approach it or could I improve on this?

import-module smlets -force
$incident = Get-SCSMObject -ClassName System.WorkItem.Incident$ | where{$_.TargetResolutionTime -ne $null -and $_.Priority -ne $null}
$activityRel = Get-SCSMRelationshipClass sactivity
$Never = Get-scsmenumeration Enum.8808d5e8c9f147f995736a3d7a88805a$

Foreach ($incident in $incident) {
   $activity = Get-SCSMRelatedObject -SMObject $incident -Relationship $activityRel|Where{$_.Type -eq 'System.WorkItem.Activity.ManualActivity'}
   if($activity -ne $NULL) {
      $activity | Set-SCSMObject -Property ScheduledEndDate -Value $incident.TargetResolutionTime
}

The problem I'm running into now is updating the Priority of the Activity to a custom Priority. I am able to update the OOB properties, but I am receiving an Enumeration Not Found error on added ones. I found a few other instances of this error on these forums and others, but couldn't find any definitive answers. I have added additional priorities to the ActivityPriorityEnum list, but I cannot seem to update them. When I use  Get-SCSMEnumeration ActivityPriorityEnum, it returns the standard values but not the custom ones. I was able to find the enum GUID for one of my list values was enum.8808d5e8c9f147f995736a3d7a88805a and so here is the code I tried using, but I was still unable to get it to work.

Thanks again for the help

   if($incident.priority -eq 3) {
      $activity | Set-SCSMObject -Property Priority -value enum.8808d5e8c9f147f995736a3d7a88805a$ }
Developer
Apr 4, 2011 at 11:25 PM

try getting the value first with the Get-SCSMEnumeration cmdlet and use the Id property:

$id = (Get-SCSMEnumeration enum.8808d5e8c9f147f995736a3d7a88805a).Id
if($incident.priority -eq 3) {
      $activity | Set-SCSMObject -Property Priority -value $id 
}


Apr 7, 2011 at 4:14 PM

Hi Jim

I tried this code along with a number of variations and cannot avoid the enumeration not found error. My only experience with getting custom list values in with scripts has been with custom properties, in which case we call the class extension guid, and not just the base class. Since this is a built in property I'm not sure how to handle it.

When I do get-scsmenumeration ActivityPriorityEnum, I get the 4 built in priorities, and none of the extended ones. I assume this is intended behavior, but when I say I want to populate -property Priority, i'm wondering if it is looking at ActivityPriorityEnum as the list of values to validate against. What are your thoughts and suggestions?

Thanks,

Nick

Apr 7, 2011 at 6:17 PM

I'd suggest running Get-SCSMEnumeration |select DisplayName, Name |sort DisplayName

Search on that list for your custom enumeration and then pick up the name to use it in your script.

Custom enumerations don't have the name of their parent... you should be seeing something like Enum.<code>, but using the method suggested above, you'll be able to search for that name by looking at the display name

Apr 10, 2011 at 8:57 PM

Hi German

Thanks for the tip. This is very useful, and I was using a much less efficient method to discover these values.

I still haven't been able to populate the Priority field with custom list values. I'm considering just extending the object to have a 'Custom Priority' field, set to a custom list of values. I know I can update a custom property with custom list values, i just haven't been able to update a built in property with custom list values. Also there aren't any workflows that trigger off the Activity Priority attribute that I'm aware of, so this workaround will do the trick, but there must be a way to get this to work. Have others been succesful with it, and is it a potential bug with my particular system?

Thanks