2

In one of my script I have the following problem: assigning to a string variable the result of a function makes the string an array. Example:

---------- (function edited for clarity)

$x=""
Function extrMail{param([string]$pFullAddr)
    [string]$wAddr = ""
    $regex = ".. omitted for brevity, check for valid mail address..."
    $pFullAddr -match $regex | Out-Null
    $wAddr = $Matches.values
    if ($wAddr -notlike "@") {
        $wAddr = "[email protected]"
    }
    return [string]$wAddr 
}    

... $x= extrMail "xy"



----------

but if I look at it it's 

$x=1 abcd $x.Count # is 2


I've made several runs, in step mode to try and find where the problem arise, but no way...
I can't understand why...
Thanks for any help
PS: I'm a seasoned system engineer, I know various programming languages but I'm really a newbie with Powershell...
10
  • 2
    Your question needs a clear example that replicates the issue, otherwise answer will be based on pure guessing. Update your question providing a minimal reproducible example. Commented Jul 9 at 18:28
  • Thx for suggestion, but the problem really is that simple, a small function which produce a STRING but the variable receiving it becomes an array. I've checked for duplicated names (frequent error, where same name defined as both a string and an array...) Stepping thru the function until last instruction which is "return $result" in debug mode £result IS a string, but the immediately following step, the assignment to the other variable ($x in my example) result in an array, debug show it as 2 lines.
    – LuigiV TH
    Commented Jul 9 at 18:34
  • your function as you have it in your question is not returning anything and we don't know what "... some code here" is. Commented Jul 9 at 18:37
  • 2
    A common cause of functions unexpectedly returning more than intended (with two or more output objects becoming an array when the output is collected) is PowerShell's implicit output behavior - see this answer.
    – mklement0
    Commented Jul 9 at 19:20
  • 1
    And: stackoverflow.com/a/74717663/1701026
    – iRon
    Commented Jul 9 at 19:35

0

Browse other questions tagged or ask your own question.