Share via

PowerShell function return parameter tmp_nqd1ogsr.jbe

Mali Stane 91 Reputation points
2022-09-23T13:51:35.263+00:00

Hi,
I have simple function that return two parameters.
Sometimes it return third parameter tmp_******.jbe
Basically I call a function :
function Updateuser {
Param
(
[Parameter(Mandatory = $true)] $SamAccountName
[Parameter(Mandatory = $true)] $Name,
**
[Parameter(Mandatory = $false)] $pathlog
)

Function logic ….

return $data1, $data2, $data3
}
$Myupdate = Updateuser $Par1 $par2 $Par4
$Myupdate[0]
$Myupdate[1]
$Myupdate[2]

Sometimes return from $Myupdate[0] is tmp_******.jbe and somtims is correct value for $data1
I don’t know what I’m doing wrong ?

Windows for business | Windows Server | User experience | PowerShell

Answer accepted by question author

Michael Taylor 61,226 Reputation points
2022-09-23T14:30:08.557+00:00

Powershell tries to figure out typing based upon the values which is, IMHO, very fragile. For example if you have a function that builds up an array of values and your array has multiple values then it works fine. But if you have a single value then it is treated as an object and array syntax fails. Similar problems occur if you have arrays within arrays.

Just guessing what your function is doing and why it would return unexpected results. I would recommend you be explicit about returning an array to avoid problems like this and see if your problem goes away.

   return @($data1, $data2, $data3)  

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Mali Stane 91 Reputation points
    2022-09-26T06:15:44.137+00:00

    Thank you for your suggestions !
    I will set “return” as array.
    Issue, when I observe this undesired behaviour, usually happens when I run script more than once.
    Script is much bigger and involve creating a new user from template or group, enable o365 mailbox etc…. and contains a lot of GUI elements. This specific problematic function is called when user support edit existing user (user got marid etc…) so type of rename operation, and then return new Distinguish name of effected user and proceed with next step (function) mail attribute modification… Issue is ,an I believe it will be solved with array, to return new distinguish name to proceed with next step of the process calling function that manipulate proxy address…
    That is just sample of code its not…

    function UpdateUserGroupMin {

    Param  
    (  
        [Parameter(Mandatory = $true)] $SamAccountName_MIN,  
        [Parameter(Mandatory = $true)] $Name_MIN,  
        [Parameter(Mandatory = $true)] $GivenName_MIN,  
        [Parameter(Mandatory = $true)] $Surname_MIN,  
        [Parameter(Mandatory = $true)] $OtherName_MIN,  
        [Parameter(Mandatory = $true)] $DisplayName_MIN,  
        [Parameter(Mandatory = $true)] $Description_MIN,  
        [Parameter(Mandatory = $true)] $Company_MIN,  
        [Parameter(Mandatory = $true)] $UPN_MIN,  
        [Parameter(Mandatory = $true)] $Mail_MIN,  
        [Parameter(Mandatory = $true)] $DN_MIN,  
        [Parameter(Mandatory = $true)] $object_MIN,  
        [Parameter(Mandatory = $false)] $Errorlogpath  
    )  
    

    *
    *

    if ($object_MIN -eq "User")  
        {  
            If (!(([string]::IsNullOrEmpty($GivenName_MIN))))  
            {  
                Set-ADUser -Identity $DN_MIN -GivenName $GivenName_MIN  
            }  
    *  
    *  
    *  
    

    }
    elseif ($object_MIN -eq "Group")
    {
    If (!(([string]::IsNullOrEmpty($DisplayName_MIN))))
    {
    Set-ADGroup -Identity $DN_MIN -DisplayName $DisplayName_MIN
    }
    *
    *
    *
    }
    Return ….

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.