Workflows with if-then, PowerShell scripts, and $work.SavedObjectProperies

Hi,

I am working on a workflow to do some automation on our computer objects, and I am running into a weird issue that I am struggling to wrap my head around.

The error I am receive:

Executing the 'Run script: DEV New Computer Object Tag Location on Search Results' activity  
 
7/11/2023 12:33:21 PM (UTC) 

•Activity 'Run script: DEV New Computer Object Tag Location on Search Results' encountered an error.  Details <<< 
An item with the same key has already been added. 

ARS Version: 7.4.3

The workflow is setup in the following way

  • The workflow will run on a schedule
  • There is a managed unit that the workflow uses to search for objects
  • The properties of the found objects are saved.
    • Currently I am saving the DistinguishedName
  • There is then an If-then statement that evaluates the DistinguishedName and will update a virtual attribute based on what is found. There are 5+ conditions being evaluated.

NOTE - Up until this point, everything works as expected

  • I then have a PowerShell script that will connect to a 3rd party service
    • Important - This is WITHIN the If-then statement and takes place immediately after the virtual attribute is updated 
    • This has not yet been tested because I am struggling with another piece. I am just included this for reference.
  • I then have a PowerShell script that will essentially check if the previous steps were successful, and if not, the script will update some virtual attributes to allow us to be alerted and cause the machine to be re-evaluated the next time the workflow runs.
    • Important - This is NOT WITHIN the if-then statement. It is set to run AFTER. 
    • This is where I receive the error above.

In the PowerShell script that triggers the error above I have managed to narrow down the exact line that is causing it.

$computers = $workflow.SavedObjectProperties("Save object properties").get("distinguishedName")

My intent with this line of code is to loop through all the objects that were originally found, and then check their attributes to see if the previous steps were successful.

What seems to be happening though is that the "$workflow.SavedObjectProperties("Save object properies")" is being changed by the If-then statement.

After the if-then statement is run, it seems only 1 computer object is returned by the above command. Even though there are two objects.

If I re-order things, and I run the PowerShell script that triggers the error BEFORE the workflow if-then statement, the PowerShell script will work and process each object found.

But this is out of order and not what I want.

Is there some way of having the PowerShell script use the object that is running through the If-then statement?

Or am I doing something else wrong? I can't figure out why the if-then statement appears to be impacting the "$computers = $workflow.SavedOjectProperies.." in the PowerShell script.

I have a few other examples where this is actually working.

It is important to note that if only 1 computer object is returned by the search operation of the workflow, then everything things to run as expected.

Not sure if any of what I just described made any sense, I hope so.

I am sure I am doing something stupid, any suggestions would be greatly appreciated.

Regards,

Todd

Parents
  • An important thing to understand about the Search Activity is that any workflow activities (be it a script or a built-in activity) that you place in the "workspace" of the search are executed for each item returned by the search.

    So, the "$computers =" line you cite above is only going to receive ONE computer at a time for processing.

    Hence why I typically don't use Searches if I have established a Managed Unit to pull together my object "queue".

    Rather, I just do this within my script:

    $ObjectList = Get-QAD<something> ...to enumerate the contents of the Managed Unit

    $ObjectList | foreach {

    <Do stuff on each object returned from the MU>

    }

  • Hey  ,

    I do something similar to this, I think. Although your way is more elegant.

    # Pull devices missing location
    $computers = $workflow.SavedObjectProperties("Save object properties").get("distinguishedName")
    
    # Process found devices
    foreach ( $computer in $computers )
    {
        <do stuff>
    }

    But before I can even get to this point, my script is failing at the "$computers =  $workflow.SavedObjectProperties..." part.

    On a side note, I decided to completely rebuild the workflow from scratch. Not copying anything from the original one, to the new one. I just re-created it step for step. Now it seems to be working. I haven't enabled every single step yet, but so far so good.

    I feel like that doesn't make any sense.

  • Can you please post a screen cap of your workflow so we can see how things are arranged?

    Why are you using "Saved Object Properties"?  Are you not trying to search the contents of your MU to get the list of computers to process?

    If yes, then you should be using $Workflow.FoundObject("Enumerate MU").get("distinguishedName")

    ...where "Enumerate MU" is a name I just made up for a Search Activity that is listing the contents of your Managed Unit.

    Again, you could just skip the Search Activity by incorporating the enumeration of the MU right in your script which would be fired directly by your workflow.

Reply
  • Can you please post a screen cap of your workflow so we can see how things are arranged?

    Why are you using "Saved Object Properties"?  Are you not trying to search the contents of your MU to get the list of computers to process?

    If yes, then you should be using $Workflow.FoundObject("Enumerate MU").get("distinguishedName")

    ...where "Enumerate MU" is a name I just made up for a Search Activity that is listing the contents of your Managed Unit.

    Again, you could just skip the Search Activity by incorporating the enumeration of the MU right in your script which would be fired directly by your workflow.

Children
  • I definitely can post a screenshot, but I am struggling a little bit to figure out how to correctly do that.

    Is there a way to upload the screenshot directly to the forum post? Or does it have to be hosted externally?

    The reason I am using "Saved Object Properties" is because that is what I have been shown in another project I worked on. It just happened to be what I am familiar with.

    Yes, I am trying to utilize the results from the search operation and perform some action on those objects.

    I am using the native "Change object property" feature of the workflow to make some changes to the object so I couldn't eliminate the search entirely. But I understand what you are saying.

    After some additional testing, it looks like my workflow has stopped working again with the same error. I will try adjusting the logic to use the "FoundObject" call instead.