Home Powershell Activity

Powershell Activity In Progress

All,

we have been working in automating a bit our services.

Case: We need to get the affected user of an request offering and update an attribute of the user in our AD.

However the script remains in Progress:


Script:

param([guid]$parentId)

Import-module ActiveDirectory    

Import-Module SMLets


# Get WorkItem

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

$parent = Get-SCSMObject -id $parentId


#Get Affected User

$UserDisplayName = ( Get-SCSMRelationshipObject -BySource $parent | where {$_.Sourceobject -like'*powershell*'} ).TargetObject.DisplayName


#Update AD attribute

$user = Get-ADUser -Filter { Displayname -eq$UserDisplayName}

Set-ADUser -Identity $user-Replace @{extensionAttribute10='true'}


Account:

It runs under an account that has admin rights in AD.


Any support is appreciated.

thank you.

George

Best Answer

Answers

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭
    edited August 27

    You're so close!

    #Get Affected User
    $UserDisplayName = (Get-SCSMRelationshipObject -BySource $parent | where {$_.Sourceobject -like'*powershell*'} ).TargetObject.DisplayName
    


    • [x] You're asking SCSM for a relationship by it's source. The source in the case being the Work Item.
    • On the pipeline you then say where the Source Object is like PowerShell
    • [x] Finally you're asking for the Target Object's DisplayName


    Steps 1 and 3 are perfectly fine, but the issue here is in step 2 - how you're asking for the relationship from the parent work item. Let's break this down a bit further:

    Get-SCSMRelationshipObject -BySource $parent
    

    Perfect. You'll get all of the relationships as it pertains to the Parent Work Item (a Service Request in this case). This command will return a list of Relationship Objects to you in PowerShell. Something like the Affected User to the SR, Assigned to User to the SR, Created By User to the SR, etc.

    | where {$_.Sourceobject -like'*powershell*'} 
    

    The syntax here is valid, but you're asking Service Manager the wrong question. Given the previous command to the left of the pipeline gave you all of the Relationship Objects (with one of them being the Affected User) we should be filtering a property that we know for certain will return a single Relationship Object. So in this case, we should filter on the RelationshipId. A guid that will never change and always point us to the Affected User. How can we get the guid of the Affected User relationship? In your own PowerShell window run the following against one of your management servers.

    Get-SCSMRelationshipClass -name "affected" -computername "managementServerHere" | select name, id
    

    This returns the unique id of dff9be66-38b0-b6d6-6144-a412a3ebd4ce. This is the guid we can now use in the pipeline.

    $UserDisplayName = (Get-SCSMRelationshipObject -BySource $parent | where-object {$_.RelationshipId -eq 'dff9be66-38b0-b6d6-6144-a412a3ebd4ce'}).TargetObject.DisplayName
    

    So now this command will give us all of the relationship objects, filter down to just the Affected User to Service Request, then select only the Target Object's (affected user's) Display Name property. The rest of the PowerShell at this point should work fine.

  • George_KouskouridasGeorge_Kouskouridas Customer IT Monkey ✭

    Adam,


    thank you for the prompt reply. Script looks like this now:

    param([guid]$parentId)

    Import-module ActiveDirectory    

    Import-Module SMLets


    # Get WorkItem

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

    $parent = Get-SCSMObject -id $parentId


    #Get Affected User

    $UserDisplayName = (Get-SCSMRelationshipObject -BySource $parent | where-object {$_.RelationshipId -eq'dff9be66-38b0-b6d6-6144-a412a3ebd4ce'}).TargetObject.DisplayName


    #Update AD attribute

    $user = Get-ADUser -Filter { Displayname -eq$UserDisplayName}

    Set-ADUser -Identity $user-Replace @{extensionAttribute10='true'}


    but still remains in Progress.

    In the logs we see the following:

    Start workflow

    Date: 27/08/2020 15:25:02

    27/08/2020 15:25:02 Initialize EMG

    27/08/2020 15:25:02 Get Instacce Id: 63fe3fd5-7911-910e-51c7-b6835786f703

    27/08/2020 15:25:02 Set Criteria

    27/08/2020 15:25:03 Get data reader

    27/08/2020 15:25:03 Get powershell script

    27/08/2020 15:25:03 Determining if script is to be executed by an account

    27/08/2020 15:25:03 Script is to be executed by an account

    27/08/2020 15:25:03 Error excuting PowerShell

    27/08/2020 15:25:03 GetSpecificAccountById error...


    Any ideas what might cause it to fail? It runs under domain admin account.


    thank you

    George

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭

    Hey there George,

    Looking at the logs you posted, the last message (GetSpecificAccountById error) references an issue from a previous version. When you can, upgrade your PowerShell Activity management pack and try this piece of automation again.

  • George_KouskouridasGeorge_Kouskouridas Customer IT Monkey ✭

    Thank you Adam,We have updated to latest version as advised, but script give still error.

    Script:

    param([guid]$parentId)

    Import-module ActiveDirectory    

    Import-Module SMLets

    # Get WorkItem

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

    $parent = Get-SCSMObject -id $parentId

    #Get Affected User

    $UserDisplayName = (Get-SCSMRelationshipObject -BySource $parent | where-object {$_.RelationshipId -eq'dff9be66-38b0-b6d6-6144-a412a3ebd4ce'}).TargetObject.DisplayName

    #Update AD attribute

    $user = Get-ADUser -Filter { Displayname -eq$UserDisplayName}

    Set-ADUser -Identity $user-Replace @{extensionAttribute10='true'}



    Error:

    Start workflow

    Date: 28/08/2020 08:07:45

    28/08/2020 08:07:45 Initialize EMG

    28/08/2020 08:07:45 Get Instacce Id: c02d02c5-6d5c-30d9-b489-6da136032341

    28/08/2020 08:07:45 Set Criteria

    28/08/2020 08:07:45 Get data reader

    28/08/2020 08:07:45 Get powershell script

    28/08/2020 08:07:45 Determining if script is to be executed by an account

    28/08/2020 08:07:45 Get property mapping

    28/08/2020 08:07:45 Deserialize property mapping xml

    28/08/2020 08:07:45 Initialize PowerShell object

    28/08/2020 08:07:45 Get activity parent

    28/08/2020 08:07:45 Add PowerShell to execute

    28/08/2020 08:07:45 Run PowerShell in normal worklfow

    28/08/2020 08:07:45 Add work item data to parameter

    28/08/2020 08:07:45 Finish adding parameters

    28/08/2020 08:07:45 Invoke PowerShell Object

    28/08/2020 08:07:49 PowerShell finish executing

    28/08/2020 08:07:49 Printing PowerShell output to Output property

    28/08/2020 08:07:49 Assigning ManuallyRunWorkflow to false

    28/08/2020 08:07:49 Powershell has error.

    Cannot bind argument to parameter 'Id' because it is null.Cannot bind argument to parameter 'BySource' because it is null.Variable: 'UserDisplayName' found in expression: $UserDisplayName is not defined.Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.

    28/08/2020 08:07:49 Start updating instance

    28/08/2020 08:07:49 Instance successfully updated


    Any ideas?

    thank you


    George

  • George_KouskouridasGeorge_Kouskouridas Customer IT Monkey ✭

    Adam,

    thank you very much for the reply.

    Trying you way works normally but i get an error when we try to update the item in our AD.

    So it is permissions related. We will figure it out.

    G.

Sign In or Register to comment.