Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell 2: Array size differs between return value and caller

I have been banging my head on this for a few hours and google is no help.

I have a function that is called in my code. The call looks like this:

$newData = @(CreateMailContactIfNeeded $entry)

The function that is called should always return a 2-element array. I confirm this right before the return statement of the called function:

# ... (inside CreateMailContactIfNeeded) ...
$tmpArr = @("$contactString" , "external")
Write-Host "length is" $tmpArr.length
Write-Host "Contact string is ->"$contactString"<-"
return $tmpArr

The length is always 2, and the contact string is always non-null. Sample output:

length is 2
Contact string is -> GMSContact-33<-

(I am unable to strip the leading space in the string, so I assume this is some sort of internal powershell string formatting or something)

HOWEVER, when I get back to the calling function, sometimes, but not always, the array that I build from the return value is length three:

if ($newData.length -eq 3) {
  Write-Host "Weird - array length is 3..."
}

Write-Host "Array length now is "$newData.length
foreach ($tmpVar in $newData) {
  Write-Host "Array entry is "$tmpVar
}

The first element of the 3-entry array is a valid string, but it is an artifact from earlier processing:

Weird - array length is 3...
Array length now is  3
Array entry is  Test User       <-- INVALID
Array entry is  GMSContact-33   <-- This should be first element
Array entry is  external        <-- This should be second element

Can someone explain to me what is possibly going on and how to fix it? Am I doing the array assignment incorrectly? Given the code above, how is it possible that "Test User" is getting inserted into the array?

EDIT: Full content of CreateMailContactIfNeeded below...

function CreateMailContactIfNeeded {
  param($CSVEntry)

  $email = $CSVEntry.EMAIL_ADDR

  Write-Host    "--> Searching for $email"
  Write-Host -n "----> In AD? "
   # Return if this person already exists as a user in AD
  if (IsInOurADDomain $CSVentry) {
    Write-Host -f green "YES."
    Write-Host "Returning "$email "= inhouse"
    return @("$email" , "inhouse")
  } else {
    Write-Host -n -f gray "NO. "
  }

  $contactString = "GMSContact-" + $CSVEntry.MEMBER_ID
  Write-Host "Contact string is now " $contactString

  # Check if mail contact already exists. If not, create it.
  Write-Host -n "In Contacts? "
  $object = Get-MailContact $email 2> $null

  if ($object -eq $null) {
    $displayName = $CSVentry.FIRST_NAME + " " + $CSVentry.LAST_NAME

    Write-Host -n -f gray "NO. "
    Write-Host -n "Creating contact... "

    $error.clear()
    New-MailContact -Name $contactString                    `
                    -DisplayName $displayName               `
                    -ExternalEmailAddress $email            `
                    -OrganizationalUnit $global:OU_CONTACT_STRING  
    if ($error[0] -ne $null) {
      Write-Host -f red "ERROR"
      Write-Error $error[0]
      return @("error","error")
    }
    # Derek says to do this to make it appear in alternate address book
    Set-MailContact -identity $contactString -customattribute1 "AB2"
    Write-Host -f green "SUCCESSFUL"
  } else {
    Write-Host -f green "YES"
  }
  Write-Host "Returning" $contactString "= external"
  $tmpArr = @("$contactString" , "external")
  Write-Host "length is" $tmpArr.length
  Write-Host "Contact string is ->"$contactString"<-"
  Write-Host "Contact string length is "$contactString.length
   foreach ($tmp in $tmpArr) {
     Write-Host "In function element is "$tmp
   }
   return $tmpArr
#  return @("$contactString" , "external")
}
like image 782
Larold Avatar asked Jan 18 '23 19:01

Larold


1 Answers

In Poweshell, anything that is not "captured" is effectively "returned".

Let's take an example:

function test(){

    "starting processing"

    #do some processing

    write-host "starting different process"
    $output = @("first",second")
    return $output
}

When you do something like $myoutput =test, you will see that $myoutput actually has three elements - first,second as expected and also starting processing. Because the starting processing was uncaptured and was "returned" to the pipeline.

Wherever invalid "TEST USER" output is generated in your script, try capturing the output or pipe it to Out-Null or assign it to $null


Easy way to figure out where the extra element is being "returned" from.

Change the script so that you do not capture the output of the function in a varaiable. So leave it as CreateMailContactIfNeeded $entry

Before running the script, run Set-PsDebug -trace 1. Now, run the script - you should be able to see where TEST USER is being returned. Capture it as suggested above.

like image 186
manojlds Avatar answered Jan 21 '23 08:01

manojlds