Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:50:35+00:00 2026-05-25T09:50:35+00:00

I have been banging my head on this for a few hours and google

  • 0

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")
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:50:36+00:00Added an answer on May 25, 2026 at 9:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

all. I've been banging my head for a few hours because of this problem.
I have been banging my head for hours trying to figure out why this
Been banging my head on this for 8 hours now... I have a need
I have been banging my head against the wall for a few hours on
I have been banging my head on this one for hours and I am
I have been banging my head on this one for too long. I have
I have been banging my head against the wall with this odd behaviour on
I have been banging my head on a wall with this one. I need
i have been banging my head for the solution for nearly 14 hours and
I have been banging my head on this (im a newbie in MySQL, be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.