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")
}
In Poweshell, anything that is not “captured” is effectively “returned”.
Let’s take an example:
When you do something like
$myoutput =test, you will see that $myoutput actually has three elements –first,secondas expected and alsostarting processing. Because thestarting processingwas 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-Nullor assign it to$nullEasy 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 $entryBefore running the script, run
Set-PsDebug -trace 1. Now, run the script – you should be able to see whereTEST USERis being returned. Capture it as suggested above.