Filter IRs, CRs and computers with PowerShell script


  • Hi all, I need to optimize a PS script but I just don't know exactly how...
    In a short: since we frequently need to rename user accounts, data duplication appears into the SM database because the userid field is a key. So...We need to reroute all the Work Items created by the old user to the new user (as well as computer ownership).
    I'm not a PS guru at all, so...I wrote a script that scans all WIs (CRs, IRs) and computers object to find the old user as the "affected user", "created by user" and the "owned by user". This is very poor in terms of performance, so...I'd like to know if there's a way to directly extract all the WIs and computers related to the old user and - on them - change the proper relationships.

    This is the code of my PS script:

    Import-Module SMLets
    $OldUserName = "user01"
    $NewUserName = "user03"
    $CreatedByRelClass = Get-SCSMRelationshipClass System.WorkItemCreatedByUser
    $AffectedUserRelClass = Get-SCSMRelationshipClass System.WorkItemAffectedUser
    $OwnedByRelClass = Get-SCSMRelationshipClass System.ConfigItemOwnedByUser
    $IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
    $ChangeRequestClass = Get-SCSMClass -Name System.WorkItem.ChangeRequest$
    $UserClass = Get-SCSMClass System.Domain.User
    $ComputerClass = Get-SCSMClass Windows.Computer$
    $NewUser = Get-SCSMObject $UserClass -Filter "UserName -eq $NewUserName"
    $Incidents = Get-SCSMObject -Class $IncidentClass
    $ChangeRequests = Get-SCSMObject -Class $ChangeRequestClass
    $Computers = Get-SCSMObject -Class $ComputerClass
    If ($Incidents.count -gt 0){
    	foreach ($Incident in $Incidents){
    		$AffectedUser = Get-SCSMRelatedObject -SMObject $Incident -Relationship $AffectedUserRelClass
    		If ($AffectedUser.UserName -eq $OldUserName){
    			write-host "Incident" $Incident.ID "matches Affected User $OldUserName"
    			New-SCSMRelationshipObject -RelationShip $AffectedUserRelClass -Source $Incident -Target $NewUser -Bulk
    			write-host "--> Affected User modified to $NewUserName"
    			write-host ""
    If ($ChangeRequests.count -gt 0){
    	foreach ($ChangeRequest in $ChangeRequests){
    		$AffectedUser = Get-SCSMRelatedObject -SMObject $ChangeRequest -Relationship $AffectedUserRelClass
    		$CreatedByUser = Get-SCSMRelatedObject -SMObject $ChangeRequest -Relationship $CreatedByRelClass
    		If ($AffectedUser.UserName -eq $OldUserName){
    			write-host "Change Request" $ChangeRequest.ID "matches Affected User $OldUserName"
    			New-SCSMRelationshipObject -RelationShip $AffectedUserRelClass -Source $ChangeRequest -Target $NewUser -Bulk            
    			write-host "--> Affected User modified to $NewUserName"
    			write-host ""
    		If ($CreatedByUser.UserName -eq $OldUserName){
    			write-host "Change Request" $ChangeRequest.ID "matches Created By User $OldUserName"
    			New-SCSMRelationshipObject -RelationShip $CreatedByRelClass -Source $ChangeRequest -Target $NewUser -Bulk            
    			write-host "--> Created By User modified to $NewUserName"
    			write-host ""
    If ($Computers.count -gt 0){
    	foreach ($Computer in $Computers){
    		$Custodian = Get-SCSMRelatedObject -SMObject $Computer -Relationship $OwnedByRelClass
    		If ($Custodian.UserName -eq $OldUserName){
    			write-host "Computer" $Computer.Name "matches Custodian $OldUserName"
    			New-SCSMRelationshipObject -RelationShip $OwnedByRelClass -Source $Computer -Target $NewUser -Bulk            
    			write-host "--> Custodian modified to $NewUserName"
    			write-host ""

    We use SM 2010 SP1 + CU3

    Thanks for any suggestion!!!

    • Edited by DashKappei Monday, November 19, 2012 12:21 PM
    Monday, November 19, 2012 12:20 PM

All replies

  • This is just a quick example of how to change an assigned user from $OldUser, to $NewUser.
    You can expand this and apply the similar steps to change other relationships.

    I didn't get to test the code below to make sure it didn't contain errors, so apologize in advance if you had to make tweaks!

    # User management pack class
    $mpcUser = Get-SCSMClass System.Domain.User$
    # Get the old user, and new user objects
    $OldUser = Get-SCSMObject $mpcUser -Filter "UserName -eq 'OldUser'"
    $NewUser = Get-SCSMObject $mpcUser -Filter "UserName -eq 'NewUser'"
    # Get the work item assigned user relationship class
    # In this case we want the Assigned To User
    $mprAssignedToUser = Get-SCSMRelationshipClass System.WorkItemAssignedToUser$
    # Get the incident management pack type projection
    # It is highly recommended you use a narrower type projection that captures
    # only the relationships you're interested in
    $mptpIncident = Get-SCSMTypeProjection System.WorkItem.Incident.ProjectionType$
    # Get all incidents
    # Feel free to apply a filter to narrow your results to save time
    # Depending on the number of incidents and the TypeProjection you specified above
    # this could take a while
    $incidents = Get-SCSMObjectProjection $mptpIncident
    # Iterate
    foreach ($incident in $incidents)
    	# If the assigned user is the old user
    	if ($incident.AssignedWorkItem -eq $OldUser)
    		$base = $incident.__base
    		# Relate the new user to the incident work item
    		$base.Add($NewUser, $mprAssignedUser.Target)
    		# Save the changes

    • Edited by georgevo Tuesday, November 27, 2012 6:16 AM
    Tuesday, November 27, 2012 6:13 AM