Workflow - Set Computer Object Attribute

Hi

Sorry for not wrapping the code below. I have tried three different browsers and two machines and nothing happens when i select Code / Powershell

The issue:

I have two VA on a computer object

First one is a DN object picker UserDNVA and the second is a Directory string called UserDNVA-DisplayName.

Now as mentioned that are both assigned to a computer object. On the Computer object properties on the Web interface i have added UserDNVA and you select the user account. All good there. I then have the script below which i am trying to use to read that user object DN value and then go and find me the display name and add it to UserDNVA-DisplayName

The workflow starts on a change to a VA being changed on the form. The first time the VA changes to TRUE the UserDNVA-DisplayName never updates with the display name. If i set the VA to Fasle , Save , then set to TRUE it will update the displayname. I assume as its already there. 

So is this a case of the attribute value has not been written to the computer object before the script runs? if so how can i get around that? I have tried adding a sleep in the script but that makes no difference at all. 

function AddLicensedUserVA($Request)
{
Connect-QADService -Service "ARSERVERNAME" -Proxy

# Get Quest ARS Computer Name
$ComputerSAM = $DirObj.get("SamAccountName")
$ComputerName = $ComputerSAM.Trim("$")

# Get only the attribute you need
$Computer = Get-QADComputer $ComputerName -DontUseDefaultIncludedProperties -IncludedProperties "UserDNVA"

$UserDN = $Computer."UserDNVA"

if ($UserDN) {

# Much faster than Get-QADUser when you already have the DN
$User = Get-QADObject -Identity $UserDN -DontUseDefaultIncludedProperties -IncludedProperties "displayName"

if ($User) {
$DisplayName = $User.DisplayName

# Write the display name back to the computer object
Set-QADObject -Identity $Computer.DN `
-ObjectAttributes @{ "UserDNVA-DisplayName" = $DisplayName }

Write-Host "Updated DisplayName attribute with: $DisplayName"
}
else {
Write-Warning "User not found for DN: $UserDN"
}
}
else {
Write-Warning "Computer attribute UserDNVA is empty."
}

}

Parents
  • Hi  

    The workflow starts on a change to a VA being changed on the form. The first time the VA changes to TRUE the UserDNVA-DisplayName never updates with the display name. If i set the VA to Fasle , Save , then set to TRUE it will update the displayname. I assume as its already there. 

    What is the start conditions for your workflow? If it's not firing when being set to true, but if when its set to false (I'm assuming the opposite logic to what you require), my assumption iks that the condition is a property of "Workflow Target" (Property of workflow target object), not "Requested Changes" ("changed value of a workflow target object property"). "Workflow Target" is akin to $DirObj, where as "Request Changes is $Request

    So is this a case of the attribute value has not been written to the computer object before the script runs? if so how can i get around that? I have tried adding a sleep in the script but that makes no difference at all. 

    My assumption here is that teh start condition needs a tweak, I'm also assuming that the script is on the pre Operation side of the workflow.

    However, not sure why you're using Get-QADComputer, nor Set-QADObject in this script, when you can use $Request to get the changes to the DN thats are listed, and $DirObj to get the existing values DN's. Then rather than updating the computer object in a seperate request, add the change into the inflight operation.

    To be honest, I'd use a "Modify Requested changes" activity step, select the property I want to change (in your case UserDNVA-DisplayName), set it to set the value, based on a script

    Then your scripts job is to create the display values,

    1) Using $DirObj to retrieve the list of DNs for UserDNVA of the existing DNs

    2) Using $Request to retrieve the list of DNs that have been added and or removed

    3) Logic to work out what the value of UserDNVA will look like after the change has completed

    4) Loop through each of the object that will be in the VA post save, and retrieve each of these DisplayName, concatenate each display name into a string

    5) REturn the concatenate string

    The return value will then be set into the UserDNVA-DisplayName as part of the request to change UserDNVA, rather than as a seperate request.

  • Thanks Stu. 

    Actually i have worked around it. In my Workflow i have a Save Object Properties and i am grabbing the DN in the Requested changes. I am then grabbing the DN in the PowerShell and using it to set the display name. This works fine. 

    function AddLicensedUserVA($Request)
    {
        #Connect-QADService -Service "Server" -Proxy
    
        # Retrieve the stored User DN from the workflow
        $UserDN = $Workflow.SavedObjectProperties("Save UserDN").get("User-License-Picker")
    
        if (-not $UserDN) {
            throw "User DN not found in SavedObjectProperties."
        }
    
        # Resolve the user object from DN
        $UserObj = Get-QADUser -Identity $UserDN
    
        if (-not $UserObj) {
            throw "Unable to resolve user object from DN: $UserDN"
        }
    
        # Extract the user's display name
        $DisplayName = $UserObj.DisplayName
    
        # Get the computer object
        $ComputerSAM = $DirObj.get("SamAccountName")
        $ComputerName = $ComputerSAM.Trim("$")
    
        $ComputerObj = Get-QADComputer -Identity $ComputerName
    
        if (-not $ComputerObj) {
            throw "Unable to resolve computer object: $ComputerName"
        }
    
        # Write the display name to the custom attribute
        Set-QADObject -Identity $ComputerObj.DN -ObjectAttributes @{
            "User-License-Picker-DisplayName" = $DisplayName
        }
    }

  • Well it was working before i went to lunch. Need to see whats going on. 

Reply Children
No Data