Home Videos

Simple PowerShell Optimizations for Maximum Value


  • Options
    Thomas_HathawayThomas_Hathaway Customer IT Monkey ✭
    When you make changes to an object with powershell do the changes happen immediately on the portal or do you need to wait for the cachebuilder to cycle? I saw the changes you made in the video but wasn't sure if that was just due to it being a local test environment. 
  • Options
    Nicholas_VelichNicholas_Velich Cireson Consultant Ninja IT Monkey ✭✭✭✭
    Updates to the Work Items via PowerShell should show up on Work Item forms almost instantly; changes to the grid views can take up to ~1 minute.

    More info on the CacheBuilder intervals here: https://support.cireson.com/KnowledgeBase/View/1176#/

    Are you seeing different results in your environment?
  • Options
    Nicholas_VelichNicholas_Velich Cireson Consultant Ninja IT Monkey ✭✭✭✭
    # Cireson Webinar 6-22-2016
    # PowerShell examples 
    # Author: Nick Velich

    ### PART 1 ###

    ## What Version of PowerShell am I using?

    ## And is that the 64-bit version of PowerShell?

    ## Aliases
    dir # Familiar to those coming from the Windows Command Line
    Get-ChildItem # Same as this
    help Get-ChildItem # This is how we know!

    ## Changing Directories
    cd C:\Users\analyst1 # For those familiar with command lines
    help cd # Can even pass in alias into help to get the actual command. Apparently that is "Set-Location"

    ## Strings, Data types, and Variables
    $one = "1"
    $two = "2"
    $total = $one + $two

    $total.GetType() # Method to determine type
    $total = [Int]$one + [Int]$two # Known as "Casting"

    ## Arrays
    $arrayExample = @()
    $arrayExample += "Thing1"
    $arrayExample += "Thing2"
    $arrayExample += 3
    $arrayExample[1] # Specifically grab an index

    ## Pipeline
    $total | Out-Host # Implicit call to Out-Host when unspecified
    $total | Get-Member # Other way to get type
    $arrayExample | Select-Object -First 2 # First two elements
    $arrayExample | Select-Object -Last 1 # Last element
    $arrayExample | Where-Object {$_.length -gt 5}
    $arrayExample | Where-Object {$_.length -gt 5} | Select-Object -Last 1

    ## Looping through an array
    foreach($item in $arrayExample){
        "This is the item: $item" | Out-Host

    for($i = 0; $i -lt $arrayExample.Length; $i++){
        "Item $i is " + $arrayExample[$i]

    ## Objects
    $hash1 = @{"Make" = "Honda"; "Model" = "Civic"; "Color" = "Blue"}
    $hash2 = @{"Make" = "Honda"; "Model" = "Accord"; "Color" = "Black"}
    $hash3 = @{"Make" = "Toyota"; "Model" = "Camry"; "Color" = "Red"}

    $car1 = New-Object PSObject -Property $hash1
    $car2 = New-Object PSObject -Property $hash2
    $car3 = New-Object PSObject -Property $hash3

    $carArray = @($car1, $car2, $car3)

    $carArray | Format-List *
    $carArray | Format-Table -Property Color,Make -AutoSize
    $carArray[0] | gm

    ### PART 2 ###

    ## Importing Cmdlets
    Import-Module SMLets # Download at: https://smlets.codeplex.com/

    ## Finding Cmdlets
    Get-Command *SCSM* # All cmdlets that have the phrase 'SCSM' in them

    ## SCSM Class Cmdlet
    Get-SCSMClass # A lot of classes
    Get-SCSMClass -Name "Incident" # A smaller list
    Get-SCSMClass -Name "System.WorkItem.Incident" # Even smaller, but how do we filter it more?
    Get-SCSMClass -Name "^System.WorkItem.Incident$" # Anchors! ^ for a front anchor, $ for a tail anchor
    $IRClass = Get-SCSMClass -Name "^System.WorkItem.Incident$"

    ## SCSM Object Cmdlet
    Get-SCSMObject -Class $IRClass # This will get ALL IRs in the environment
    Get-SCSMObject -Class $IRClass -Filter "DisplayName -like '*Test*'"
    Get-SCSMObject -Class $IRClass -Filter "DisplayName -eq 'IR4061 - Test'"
    $IR4061 = Get-SCSMObject -Class $IRClass -Filter "DisplayName -eq 'IR4061 - Test'"

    ## Format-List
    $IR4061 | Format-List -Property * # Pipeline!
    $IR4061 | fl * # Aliases, positional parameters

    ## Set SCSM Object Property
    Set-SCSMObject -SMObject $IR4061 -Property Title -Value "Whoah, a webinar just changed this"

    ## Enumerations (lists)
    Get-SCSMEnumeration # All enumerations
    Get-SCSMEnumeration | Select -First 5 # Let's look at a smaller list
    Get-SCSMEnumeration | Where-Object {$_.DisplayName -like "*IR Support Group*"}
    Get-SCSMEnumeration | Where-Object {$_.DisplayName -eq "IR Support Group 1"}
    $IRSupportGroup1 = Get-SCSMEnumeration | Where-Object {$_.DisplayName -eq "IR Support Group 1"}

    $IR4061 | fl *
    Set-SCSMObject -SMObject $IR4061 -Property TierQueue -Value $IRSupportGroup1

    ### PART 3 ###
    Get-SCSMClass -Name Cireson
    Get-SCSMClass -Name Cireson.ConsoleApps.Tier.Mappings.Settings
    $TierMappingsClass = Get-SCSMClass -Name Cireson.ConsoleApps.Tier.Mappings.Settings

    Get-SCSMObject -Class $TierMappingsClass
    Get-SCSMObject -Class $TierMappingsClass | fl *

    $All_Mappings = Get-SCSMObject -Class $TierMappingsClass 
    [xml]$All_Mappings_XML = $All_Mappings.IncidentMappings # If we do not cast as XML, a String type object will be returned. We can use Get-Member to see this
    $All_Mappings_Objs = $All_Mappings_XML.SelectNodes("//MappedGroup")

    $FirstMapping = $All_Mappings_Objs[0]
    Get-SCSMObject -Id $FirstMapping.GroupId
    Get-SCSMEnumeration -Id $FirstMapping.EnumerationId
    (Get-SCSMObject -Id $FirstMapping.GroupId).DisplayName
    (Get-SCSMEnumeration -Id $FirstMapping.EnumerationId).displayname

    foreach($Mapping in $All_Mappings_Objs){
        (Get-SCSMObject -Id $Mapping.GroupId).DisplayName + " is mapped to " + (Get-SCSMEnumeration -Id $Mapping.EnumerationId).displayname

    ### PART 4 ###

    Function this-is-a-sample-function($thisIsAParam) {
        "I am in the function, and my param is $thisIsAParam"

    this-is-a-sample-function -thisIsAParam "hello world"

    # Input: $ThisIsAParam - String to use in output message
    # Output: $Success - Returns true if the input param was not null and the message displayed, false otherwise
    # Description: Takes in a String param and writes a message to the console. If successful, it will return true.
    Function Get-BetterNamingConvention($thisIsAParam) {
        [Boolean]$Success = $false

        if($thisIsAParam -ne $null){
            "I am in the function, and my param is $thisIsAParam"
            $Success = $true
        return $Success

    Get-BetterNamingConvention -thisIsAParam $null
    Get-BetterNamingConvention -thisIsAParam "hello!"

    # Input: Username of user to add as a reviewer, and the RA object in which they will be added
    # Output: None
    # Description: Creates necessary reviwer-review activity relationships to add the user as a reviwer
    Function fn-Add-UserAsReviewer {
    param($username, `
                $veto = $false, `
                $mustvote = $false, `

    $id_adUserClass = '10a7f898-e672-ccf3-8881-360bfb6a8f9a'
    $class_adUser = get-scsmclass -id $id_adUserClass
    $scsmUser = get-scsmobject -class $class_adUser -filter "username -eq $username"

    #Direct reviewer add SCSM user by guid
            $rel_HasReviewer = Get-SCSMRelationshipClass -name "System.ReviewActivityHasReviewer"
            $rel_ReviewerIsUser = Get-SCSMRelationshipClass -name "System.ReviewerIsUser"
            $class_ReviewerClass = Get-SCSMClass -name "System.Reviewer$"

            $Reviewer = $null
            $ReviewerArgs = @{ReviewerID = "{0}"; Mustvote = $mustvote; Veto = $veto}

            $Reviewer = New-SCSMObject -class $class_ReviewerClass -propertyhashtable $ReviewerArgs -nocommit
            $reviewerStep1 = New-SCSMRelationshipObject -nocommit -Relationship $rel_HasReviewer -Source $activity -Target $Reviewer
            $reviewerStep2 = New-SCSMRelationshipObject -nocommit -Relationship $rel_ReviewerIsUser -Source $Reviewer -Target $scsmUser

    Get-SCSMClass -name "Review"
    Get-SCSMClass -name "System.WorkItem.Activity.ReviewActivity"
    $RAClass = Get-SCSMClass -name "System.WorkItem.Activity.ReviewActivity"
    Get-SCSMObject -Class $RAClass -Filter "Id -eq RA7188"
    $RA7188 = Get-SCSMObject -Class $RAClass -Filter "Id -eq RA7188"

    $Username = "enduser2"

    fn-Add-UserAsReviewer -username $Username -activity $RA7188

    ### PART 5 ###

    # Input: 
    #        Title - String for the title of the new RA
    #        SequenceId - Int for the Sequence Id of the new RA
    #        SR_Obj - Service Request Object the new RA will be added to
    # Output: None
    # Description: This function creates a new RA with the specified params, and adds a user with the username 'enduser3' to the newly added RA
    Function fn-Create-RAinSR($Title, [int]$SequenceId, $SR_Obj){
        $ApprovalEnum = Get-SCSMEnumeration -Name "ApprovalEnum.Unanimous" # Enum for Approval Condition. Hard coding Unanimous here.
        $Rel_WIContainsActivity = Get-SCSMRelationshipClass -Name "System.WorkItemContainsActivity" # Relationship between SRs and Activities

        $RA_Class = Get-SCSMClass -Name "System.WorkItem.Activity.ReviewActivity$"
        $RA_Hash = @{Title = $Title; SequenceId = $SequenceId; ID = 'RA{0}'; ApprovalCondition = $ApprovalEnum} # Hash example
        $RA_Obj = New-SCSMObject -Class $RA_Class -PropertyHashtable $RA_Hash -NoCommit # New object; NoCommit because the object and relationship must be committed simultaneously 
        $RelObj = New-SCSMRelationshipObject -Relationship $Rel_WIContainsActivity -Source $SR_Obj -Target $RA_Obj -NoCommit # New relationship

        $RelObj.commit() # Commit them both!
        fn-Add-UserAsReviewer -username "enduser3" -activity $RA_Obj # Lets add a user too

    Get-SCSMClass -Name "ServiceRequest"
    Get-SCSMClass -Name "System.WorkItem.ServiceRequest"
    Get-SCSMClass -Name "System.WorkItem.ServiceRequest$"
    $SRClass = Get-SCSMClass -Name "System.WorkItem.ServiceRequest$"

    Get-SCSMObject -Class $SRClass -Filter "Id -eq SR7682"
    $SRObj = Get-SCSMObject -Class $SRClass -Filter "Id -eq SR7682"

    fn-Create-RAinSR -Title "Test Added RA" -SequenceId 2 -SR_Obj $SRObj
Sign In or Register to comment.