Feb
24

SMLets – Enum and wildcards

By

I’ve seen the question two times now, and that’s two times too many :)
The script below will show you how you can locate all Incident (as an example) that has an Classification value matching a Incident Classification that has a display name that matches *problem*.

?View Code POWERSHELL
Import-module smlets -Force
 
$class = get-scsmclass system.workitem.incident$
 
#Try to narrow the next search down by getting the root value for the enums you're interested in.
$rootEnum = Get-SCSMEnumeration IncidentClassificationEnum$
 
#Might be performance heavy depending on how many child enums the parent enum has.
$matchingEnums = get-scsmchildenumeration -Enumeration $rootEnum|?{$_.DisplayName -like '*problem*'}
 
$Filter = ""
$matchingEnums|%{$Filter +="Classification -eq '$($_.Id)' -or " }
$Filter = $Filter.TrimEnd("-or ")
 
Get-SCSMObject $class -Filter $Filter

Remember that the display name used when matching the filter is the localized display name used by the current process (if you run this in a workflow).
If you haven’t already got it, you’ll find SMLets at http://smlets.codeplex.com

Comments

  1. Vinh says:

    Great powershell script!

    How to do add a condition that search for the title field as well?

  2. Keang says:

    Can you help to embed the following powershell script to your?

    $class = Get-SCSMclass system.workitem.incident$
    Get-SCSMObject -class $class|?{$_.Title -like ‘*printer*’}

    I wanted to able to search for an additional condition.
    Thank you so much!
    Keang

  3. Patrik Sundqvist says:

    Hi guys!

    Just replace the following line:
    $Filter = $Filter.TrimEnd(“-or “)

    …with the line below
    $Filter = “(” + $Filter.TrimEnd(“-or “) + “) -and Title -like ‘*printer*’”

    Note: If your looking for an exact “Title” match you should use “-eq” instead of “-like” and remove the “*” (wildcard symbols).

  4. Keang says:

    Again, thank you so much for helping!

    One last question, how do you insert a value in the Description property. Is this how you do it?

    $Filter = “(” + $Filter.TrimEnd(“-or “) + “) -and Title -like ‘*printer*’” | Set-SCSMObject -Property ‘Description’ -value ‘This is a printer issue’

  5. Patrik Sundqvist says:

    Yes, that will update the Description property on all objects returned by the query. :)

  6. Patrik Sundqvist says:

    Keang, if you want to append text instead of replacing the complete text:

    Get-SCSMObject $class -Filter $Filter|%{Set-SCSMObject -SMObject $_ -Property ‘Description’ -Value ($_.Description + ” appended text..”)}

  7. Keang says:

    Patrick,

    I have tried many times to run the following script and it freezed on every attempt. Could you please help?

    Import-module smlets -Force

    $class = get-scsmclass system.workitem.incident$

    #Try to narrow the next search down by getting the root value for the enums you’re interested in.
    $rootEnum = Get-SCSMEnumeration IncidentClassificationEnum$

    #Might be performance heavy depending on how many child enums the parent enum has in the Category field.
    $matchingEnums = get-scsmchildenumeration -Enumeration $rootEnum|?{$_.DisplayName -like ‘*printer*’}

    $Filter = “”
    $matchingEnums|%{$Filter +=”Classification -eq ‘$($_.Id)’ -or ” }

    $Filter = “(” + $Filter.TrimEnd(“-or “) + “) -and Title -like ‘*hardware*’”

    Get-SCSMObject $class -Filter $Filter|%{Set-SCSMObject -SMObject $_ -Property ‘Comment’ -Value ($_.Comment + “appended text..”)}

    Get-SCSMObject $class -Filter $Filter

  8. Patrik Sundqvist says:

    Hi Keang,

    Have you added a custom property called “comment” or are you refering to a user/analyst comment. If you’re refering to a user/analyst comment then you need to change your approach since those comments are separate objects on their own, added as related objects to an incident.

  9. Keang says:

    Hello Patrik,

    I tried with the “Description” field as well and still the script did not get executed. It just freezing. There might be some typo in the script that only the expert can find them. When you get a chance, please take a look at the script again. Thank you.

    Import-module smlets -Force

    $class = get-scsmclass system.workitem.incident$

    #Try to narrow the next search down by getting the root value for the enums you’re interested in.
    $rootEnum = Get-SCSMEnumeration IncidentClassificationEnum$

    #Might be performance heavy depending on how many child enums the parent enum has in the Category field.
    $matchingEnums = get-scsmchildenumeration -Enumeration $rootEnum|?{$_.DisplayName -like ‘*printer*’}

    $Filter = “”
    $matchingEnums|%{$Filter +=”Classification -eq ‘$($_.Id)’ -or ” }

    $Filter = “(” + $Filter.TrimEnd(“-or “) + “) -and Title -like ‘*hardware*’”

    Get-SCSMObject $class -Filter $Filter|%{Set-SCSMObject -SMObject $_ -Property ‘Description’ -Value ($_.Description + ” appended text..”)}

    Get-SCSMObject $class -Filter $Filter

  10. Keang says:

    Patrik,

    Thank you so much for your effort. Your script works really well in our test environment. We were able to see the changes to the description field the next day. This is how it supposes to work where the data gets refresh overnight?

  11. David says:

    I am very interest in how you would modify the script to append a text in the user/analyst comment instead of in the description field.

    Thanks,
    David

Leave a Reply