locked
PowerShell Workflow Issue? RRS feed

  • Question

  • Hello,

    I am having an issue passing a variable from one workflow to another. If I use a hard-coded variable, it seems to work without issue. Basically, I have two workflows: one that grabs information about the user from Active Directory, and the other that updates a SharePoint list based on that returned data. Below are my scripts:

    Workflow Get-User {
    
        param(
            [string]$SamAccountName
        )
    
        $DomainController = Get-AutomationVariable -Name "Domain Controller"
        $Credentials = Get-AutomationPSCredential -Name "Administrator"
    
        $User = InlineScript {
            Get-ADUser -Filter { SamAccountName -eq $Using:SamAccountName }
        } -PSComputerName $DomainController -PSCredential $Credentials
    
        $ObjectGUID = $User.ObjectGUID
    
        Update-Employee -ObjectGUID $ObjectGUID -TelephoneNumber "+12312345678"
    
    }


    workflow Update-Employee {
        param(
            [string]$ObjectGUID,
            [string]$TelephoneNumber
        )
    
        $SharePoint = Get-AutomationVariable -Name "SharePoint"
        $Credentials = Get-AutomationPSCredential -Name "SharePoint Farm"
    
        inlinescript {
            Add-PSSnapin Microsoft.SharePoint.PowerShell
    
            $Web = Get-SPWeb -Site http://sharepoint
            $List = $Web.Lists["List"]
            $Item = $List.Items | Where-Object {$_['Object_x0020_GUID'] -eq $Using:ObjectGUID}
            $Item["Work_x0020_Phone"] = $Using:TelephoneNumber
            $Item.Update()
            $Web.Dispose()
    
        } -PSComputerName $SharePoint -PSCredential $Credentials -PSAuthentication CredSSP
    }

    When I run...

    Get-User -SamAccountName jdoe

    I receive the following error:

    "Cannot index into a null array"

    Any help?

    Friday, March 27, 2015 3:30 AM

Answers

  • Hi HHancock,

    Please try the nested workflow, and check if this issue persists:

    Workflow Get-User {
    
        param(
            [string]$SamAccountName
        )
    
        $DomainController = Get-AutomationVariable -Name "Domain Controller"
        $Credentials = Get-AutomationPSCredential -Name "Administrator"
    
        $User = InlineScript {
            Get-ADUser -Filter { SamAccountName -eq $Using:SamAccountName }
        } -PSComputerName $DomainController -PSCredential $Credentials
    
    
    ###########################################################
    
    workflow Update-Employee {
        param(
            [string]$ObjectGUID,
            [string]$TelephoneNumber
        )
    
        $SharePoint = Get-AutomationVariable -Name "SharePoint"
        $Credentials = Get-AutomationPSCredential -Name "SharePoint Farm"
    
        inlinescript {
            Add-PSSnapin Microsoft.SharePoint.PowerShell
    
            $Web = Get-SPWeb -Site http://sharepoint
            $List = $Web.Lists["List"]
            $Item = $List.Items | Where-Object {$_['Object_x0020_GUID'] -eq $Using:ObjectGUID}
            $Item["Work_x0020_Phone"] = $Using:TelephoneNumber
            $Item.Update()
            $Web.Dispose()
    
        } -PSComputerName $SharePoint -PSCredential $Credentials -PSAuthentication CredSSP
    }
    
    ###########################################################
    
        $ObjectGUID = $User.ObjectGUID
    
        Update-Employee -ObjectGUID $ObjectGUID -TelephoneNumber "+12312345678"
    
    }
    
    
    

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang



    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Marked as answer by hhancock Wednesday, April 1, 2015 2:45 PM
    Tuesday, March 31, 2015 9:02 AM

All replies

  • Just looking at the code, I'm guessing that the error occurs here:

    $Item = $List.Items | Where-Object {$_['Object_x0020_GUID'] -eq $Using:ObjectGUID}
    $Item["Work_x0020_Phone"] = $Using:TelephoneNumber
    If your Where-Object filter doesn't match anything, then $Item will be null. Then you try to access a property of it, but get exception. Try adding a check to see the state of $Item before you access it.


    Noah Stahl | Automys | Downloadable Microsoft automation examples and solutions

    • Proposed as answer by AnnaWY Tuesday, March 31, 2015 8:10 AM
    Tuesday, March 31, 2015 2:01 AM
  • Hi HHancock,

    Please try the nested workflow, and check if this issue persists:

    Workflow Get-User {
    
        param(
            [string]$SamAccountName
        )
    
        $DomainController = Get-AutomationVariable -Name "Domain Controller"
        $Credentials = Get-AutomationPSCredential -Name "Administrator"
    
        $User = InlineScript {
            Get-ADUser -Filter { SamAccountName -eq $Using:SamAccountName }
        } -PSComputerName $DomainController -PSCredential $Credentials
    
    
    ###########################################################
    
    workflow Update-Employee {
        param(
            [string]$ObjectGUID,
            [string]$TelephoneNumber
        )
    
        $SharePoint = Get-AutomationVariable -Name "SharePoint"
        $Credentials = Get-AutomationPSCredential -Name "SharePoint Farm"
    
        inlinescript {
            Add-PSSnapin Microsoft.SharePoint.PowerShell
    
            $Web = Get-SPWeb -Site http://sharepoint
            $List = $Web.Lists["List"]
            $Item = $List.Items | Where-Object {$_['Object_x0020_GUID'] -eq $Using:ObjectGUID}
            $Item["Work_x0020_Phone"] = $Using:TelephoneNumber
            $Item.Update()
            $Web.Dispose()
    
        } -PSComputerName $SharePoint -PSCredential $Credentials -PSAuthentication CredSSP
    }
    
    ###########################################################
    
        $ObjectGUID = $User.ObjectGUID
    
        Update-Employee -ObjectGUID $ObjectGUID -TelephoneNumber "+12312345678"
    
    }
    
    
    

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang



    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Marked as answer by hhancock Wednesday, April 1, 2015 2:45 PM
    Tuesday, March 31, 2015 9:02 AM
  • Just looking at the code, I'm guessing that the error occurs here:

    $Item = $List.Items | Where-Object {$_['Object_x0020_GUID'] -eq $Using:ObjectGUID}
    $Item["Work_x0020_Phone"] = $Using:TelephoneNumber
    If your Where-Object filter doesn't match anything, then $Item will be null. Then you try to access a property of it, but get exception. Try adding a check to see the state of $Item before you access it.


    Noah Stahl | Automys | Downloadable Microsoft automation examples and solutions


    Thanks for the reply. Unfortunately, it works fine if I specify the Object GUID as the ObjectGUID parameter so I don't think that's the case. I do think it's just something with the way the workflow engine is converting this workflow.
    Tuesday, March 31, 2015 12:58 PM
  • Hi HHancock,

    Have you tried the script I posted?

    If there is any update on this issue, please feel free to let us know.

    If you have any feedback on our support, please click here.

    Best Regards,

    Anna Wang

    TechNet Community Support


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    Wednesday, April 1, 2015 2:05 AM
  • I haven't had an opportunity to try this yet. Hopefully, I will get a chance tomorrow.
    Wednesday, April 1, 2015 2:59 AM
  • Hi HHancock,

    Please try the nested workflow, and check if this issue persists:

    Workflow Get-User {
    
        param(
            [string]$SamAccountName
        )
    
        $DomainController = Get-AutomationVariable -Name "Domain Controller"
        $Credentials = Get-AutomationPSCredential -Name "Administrator"
    
        $User = InlineScript {
            Get-ADUser -Filter { SamAccountName -eq $Using:SamAccountName }
        } -PSComputerName $DomainController -PSCredential $Credentials
    
    
    ###########################################################
    
    workflow Update-Employee {
        param(
            [string]$ObjectGUID,
            [string]$TelephoneNumber
        )
    
        $SharePoint = Get-AutomationVariable -Name "SharePoint"
        $Credentials = Get-AutomationPSCredential -Name "SharePoint Farm"
    
        inlinescript {
            Add-PSSnapin Microsoft.SharePoint.PowerShell
    
            $Web = Get-SPWeb -Site http://sharepoint
            $List = $Web.Lists["List"]
            $Item = $List.Items | Where-Object {$_['Object_x0020_GUID'] -eq $Using:ObjectGUID}
            $Item["Work_x0020_Phone"] = $Using:TelephoneNumber
            $Item.Update()
            $Web.Dispose()
    
        } -PSComputerName $SharePoint -PSCredential $Credentials -PSAuthentication CredSSP
    }
    
    ###########################################################
    
        $ObjectGUID = $User.ObjectGUID
    
        Update-Employee -ObjectGUID $ObjectGUID -TelephoneNumber "+12312345678"
    
    }
    
    

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang



    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    This seems to work; however, isn't the whole point of creating a runbook to make it as modular as possible? Seems to me these two workflows should work the same if they aren't nested. Am I missing something?
    Wednesday, April 1, 2015 2:44 PM
  • Hi HHancock,

    Maybe this article can give you some tips:

    PowerShell Workflows: Nesting

    Best Regards,

    Anna Wang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Edited by AnnaWY Thursday, April 2, 2015 2:31 AM
    Thursday, April 2, 2015 2:30 AM