Home General Discussion

How to store workitem objects in an array in powershell ?

Stephane_BouillonStephane_Bouillon Customer Advanced IT Monkey ✭✭✭
Hi,

Probably quite a basic question, but I'm struggling to find the right syntax. In my workflow I retrieve a few workitem objects that I want to reference later (to create child activities in them). How can I create an array, list or hashtable that contains a string as name to identify the item and a workitem object as value ?

So for example I have a SR that is for IT that I would like to reference like $ParentItem = $Items("IT").WorkItem and then create child objects in that $parentItem, where "IT" can be another string to identify an SR in another support group's queue
such as $ParentItem = $Items("HR").WorkItem

I know how to retrieve the workitems, but I can't get my head around the syntax of how to stuff it in the array.

Answers

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭
    edited August 2018
    If you're looking to add objects to an array, I think the easiest way to show this off is with a foreach loop. I apologize for the formatting. For whatever reason everytime I try to format as "Code" within the editor here it actually render's worse.

    $array = @()

    $irClass = get-scsmclass -name "system.workitem.incident$"
    $workItems = get-SCSMobject -class $irClass -filter "CreatedDate -gt '8-1-2018'"

    foreach ($workItem in $workItems)
    {
    $array += $workItem
    }


    So first you initialize a null array. Then you get your Work Item objects, for simplicity sake I went with any Incident created after August 1st. Then for-each Work Item retrieved, add it into the array using the +=. The array is now "loaded" so to speak with Incident objects that can be individually addressed, iterated through, and further properties retrieved on them.

    The array could then be addressed like:

    $array[0]

    This represents the first Incident object in the array.

    $array[0].Title

    Represents the first Incident object's Title in the array, etc. etc. Which means you could now while loop this and do something like:

    $x = 0
    while ($x -le $array.count)
    {
    if ($array[$x].SupportTier -eq 'HR')
    {
    #do stuff
    }
    if ($array[$x].SupportTier -eq 'IT')
    {
    #do other stuff
    } $x++
    }
  • Stephane_BouillonStephane_Bouillon Customer Advanced IT Monkey ✭✭✭
    Thanks for your time and effort Adam. Is there a structure in PS available that would allow me to do Something like $Array["HR"] ? That's really what I'm after. Something like an array of name value pairs that allows you to access an item's value directly by providing its name, and where the value is an object.
  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭
    I haven't put this to Code yet, but that sounds more like a hashtable
Sign In or Register to comment.