Strange Issue With Powershell Function Returning Values

JAMES, Clinton 0 Reputation points
2023-05-02T00:17:42.47+00:00

Hi,

I have created a function that is imported into powershell which is designed to return an array. Position 0 of array is a hash table and position 1 is an array

This function works great if it is imbedded into the calling code, however, not being in the calling code and being accessed due to import, it doesn't. Embedded, it returns what I would expect in position 0 and 1, but not embedded it says the returned object has a count of 6 items and the first 4 items are empty. it's not until items 5 and 6 that the object i am expecting are existing. No matter how i have tried to rework this, i cannot get it to return just 2 objects without the padding it seems to be doing. I don't desire to embed the function so am trying to work out what is happening.

Thanks in advance

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 45,431 Reputation points
    2023-05-02T02:15:31.8966667+00:00

    How about showing your code? How is it different to what's below?

    Here's a function:

    function a{
        $h=@{a=1;b=2;c=3}
        $a = $h,@('x','y','z')
        $a
    }
    
    

    Here's the code that uses that function:

    . c:\junk\fa.ps1
    $r = a
    
    

    Here's the result:

    PS C:\Junk> $r[0]
    Name                           Value
    ----                           -----
    c                              3
    b                              2
    a                              1
    PS C:\Junk> $r[1]
    x
    y
    z
    PS C:\Junk> 
    

  2. Limitless Technology 44,071 Reputation points
    2023-05-02T11:51:28.5266667+00:00
    
    Hello there,
    
    May be a piece of your code might be helpful in getting insights here.
    
    By default, function outputs are enumerated if they are collections, and enumerating an empty list yields "nothing" (more precisely, a special "null collection" value, [System.Management.Automation.Internal.AutomationNull]::Value).
    
    In order to output your list as-is, as a single object, wrap it in an auxiliary array:
    
    function foo { 
        $ret = [System.Collections.Generic.List[long]]::new()
        , $ret  # construct a single-element wrapper array around the list
    }
    
    $k = foo
    $k.GetType().Name
    The above yields List`1, proving that the list was returned.
    The aux. wrapper array was enumerated, outputting just the list.
    
    Hope this resolves your Query !!
    
    --If the reply is helpful, please Upvote and Accept it as an answer--
    
    0 comments No comments

  3. Rich Matheisen 45,431 Reputation points
    2023-05-02T15:01:21.2566667+00:00

    Maybe I'm not understanding your problem. Or maybe there's something in the functions' code you haven't disclosed. Either way, I don't experience the problem:

    This is how I invoked the function:

    # just some dummy tenants for testing
    $t =    [PSCustomObject]@{Name = '111'},
            [PSCustomObject]@{Name = '222'}
    
    . c:\junk\fa.ps1    # dot-source the function
    $r = a $t           # pass the tenants to function "a"
    
    # walk through the returned hash in the 1st element of the returned array
    $r[0].GetEnumerator()|foreach-object{
        "Name is: $($_.Key)`tPassword is: $($_.Value)"
    }
    # Do the same for the returned array in the 2nd position
    Foreach ($e in $r[1]){
        "Tenant is: $($e.Name)"
    }
    
    

    Here's the function:

    function a{
        [CmdletBinding()]
        param (
            [PSCustomObject[]]
            $Tenants
        )
        $UiDict=@{}
        $TenantInfo=@()
        FOREACH($Tenant in $Tenants)
            {
            $Account_to_seek="Z$($Tenant.name)"
            #TRY{$TenantPW=Get-CC $Account_to_seek -BypassUserCheck TRUE} CATCH {}
            [string]$TenantPW = Get-Random      # don't have an actual tenant, so provide a random password
            IF(![string]::IsNullOrWhiteSpace($TenantPW))
                {
                $UiDict."$($Tenant.Name)" = "$Account_to_seek/$tenantPw"
                $TenantInfo+=$Tenant
                }
            }
    
        Return $UiDict,$TenantInfo
    }
    
    

    And here's the result of processing the value returned from the function:

    PS C:\Junk> . 'C:\Junk\Untitled-4.ps1'
    Name is: 111    Password is: Z111/568345342
    Name is: 222    Password is: Z222/1677127922
    Tenant is: 111
    Tenant is: 222
    
    0 comments No comments