none
RunPowerShellScript Null Reference exception when trying to set Return Value RRS feed

  • Question

  • WAL (2.20.0723.0)

    I have a powershell activity that looks for the AD account of the workflow target and do some 'stuff' to it.  The powershell is in the workflow definition and uses ldap (since MIM can't actually do AD powershell natively.)  If the script finds the AD account, all the 'stuff' gets done without any problem.   I'm trying to capture and return whether the user was actually found with a simple true/false, so I can do something else another workflow activity.  Every time I try to return a value, MIMWAL logs a null reference exception: 

    Exception in 'RunPowerShellScript : RunScript'. Details: System.NullReferenceException: Object reference not set to an instance of an object.
       at MicrosoftServices.IdentityManagement.WorkflowActivityLibrary.Activities.RunPowerShellScript.RunScript(String script, IEnumerable scriptArguments, Dictionary`2 scriptParameters).

    I am still quite new to MIM, and this is my first time try to return data with powershell, so I need a sanity check that I have formatted this correctly.  

    Script (simplified):

    ..stuff to lookup the user
    If($user -eq $null)
    {$scriptpass = $false}
    else
    {  ..do stuff
    $scriptpass = $true}
    Return $scriptpass


    Script Return Type = Single Value
    Return Value Lookup = [//WorkflowData/scriptpass]

    Again, the script actually executes everything I want, despite the error message; it just doesn't pass the return value.  I have also confirmed that the script does not return any other output to screen by testing it in external sessions.  Any help is appreciated.

    Friday, October 23, 2020 10:26 PM

Answers

  • Try "void"ing or "out-null"ing the four calls on the user and group objects OR do a proper test in PowerShell IE by wrapping the script code inside a function and check the return value returned by this wrapper function. It should tell you that you are returning more than just $true even though it may not be visible.
    • Marked as answer by jamesking5 Monday, October 26, 2020 10:43 PM
    Sunday, October 25, 2020 3:40 PM
    Owner

All replies

  • What happens if you have a single liner script "return $true" and nothing else in the script? Do you still get any exception?
    Saturday, October 24, 2020 12:23 PM
    Owner
  • Running the script with just the return value works.  When I put the rest of the script back in, the null reference error returns.  I read that if a MIMWAL script returns anything besides just the expected return value that it will fail to store it.  I have tested my script in ISE and running in PS 2.0, and it only ever returns the expected value.  There are no errors or other output.  How can I tell what MIMWAL is choking on?  

    Here's what my actual script:

    Param($AN,$AP)
    
    $root = [ADSI]''
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
    $searcher.Filter = "(&(objectClass=user)(sAMAccountName=$AN))"
    $user = $searcher.FindOne()
    
    if ($user -ne $null) 
    {
    $userPath = $user.Path
    $accountToSetPW = [ADSI]$userPath
    $accountToSetPW.SetPassword($AP)
    $accountToSetPW.put("pwdLastSet", -1)
    $accountToSetPW.SetInfo()
    
    $group = [ADSI]"LDAP://CN=TG O365 Lic 1,OU=MIMTest,DC=FOO,DC=ORG"
    $group.Add($userpath)
    
    $scriptpass = $true
    }
    else 
    {
    $scriptpass = $false
    }
    Return $scriptpass

    Saturday, October 24, 2020 7:03 PM
  • Try "void"ing or "out-null"ing the four calls on the user and group objects OR do a proper test in PowerShell IE by wrapping the script code inside a function and check the return value returned by this wrapper function. It should tell you that you are returning more than just $true even though it may not be visible.
    • Marked as answer by jamesking5 Monday, October 26, 2020 10:43 PM
    Sunday, October 25, 2020 3:40 PM
    Owner
  • I was not able to get the script to generate any error output that I could examine in ISE, but I re-wrote the calls as functions and nulled the output of each function.  Mimwal was then able to process the script and get the clean return value.  Thank you for your assistance.
    Monday, October 26, 2020 10:43 PM
  • it should have been quick to test your original script and output in PowerShell ISE as below:

    function foo

    {

        $AN = "HardCodedSAM"
        $AP = "HardCodedPwd"

        ## rest of the original script

    }

    $r = foo
    $r.Count
    $r -join "|"


    Wednesday, October 28, 2020 9:57 AM
    Owner