문제

I'm trying to pass an array of custom objects to a function for further processing of these objects.

Here's the function where I create my custom object array:

Function GetNetworkAdapterList
{
    # Get a list of available Adapters
    $hnet = New-Object -ComObject HNetCfg.HNetShare
    $netAdapters = @()
    foreach ($i in $hnet.EnumEveryConnection)
    {   
        $netconprop = $hnet.NetConnectionProps($i)
        $inetconf = $hnet.INetSharingConfigurationForINetConnection($i)

        $netAdapters += New-Object PsObject -Property @{
                Index = $index
                Guid = $netconprop.Guid
                Name = $netconprop.Name
                DeviceName = $netconprop.DeviceName
                Status = $netconprop.Status
                MediaType = $netconprop.MediaType
                Characteristics = $netconprop.Characteristics
                SharingEnabled = $inetconf.SharingEnabled
                SharingConnectionType = $inetconf.SharingConnectionType
                InternetFirewallEnabled = $inetconf.InternetFirewallEnabled
                SharingConfigurationObject = $inetconf
                }
        $index++
    }   
    return $netAdapters
}

Then in my main code I call above function like this:

$netAdapterList = GetNetworkAdapterList

The $netAdapterList returns the expected data, and I can do stuff like:

$netAdapterList | fl Name, DeviceName, Guid, SharingEnabled

So far so good.

Now I want to call a function passing in the $netAdapterList

I've created a dummy function like this:

Function ShowAdapters($netAdapterListParam)
{
   $netAdapterListParam | fl Name, DeviceName, Guid, SharingEnabled
}

And when I invoke it like this:

ShowAdapters $netAdapterList

Nothing gets printed out.

I've tried changing the function's signature but still no luck:

Function ShowAdapters([Object[]]$netAdapterListParam)

Function ShowAdapters([Object]$netAdapterListParam)

Function ShowAdapters([PSObject[]]$netAdapterListParam)    

Function ShowAdapters([array]$netAdapterListParam)

Anybody knows what I'm doing wrong? How can I get to my custom objects inside the function?

도움이 되었습니까?

해결책

Thanks for your reply @Christian. Tried your steps, copy pasting bits and pieces into the shell and it indeed worked. However If i run the full .ps1 script is not printing out anything.

I've run the script in Powershell IDE setting breakpoints inside the ShowAdapters function, and $netAdapterListParam has indeed the expected custom objects array I'm passing in, so I've narrowed down the issue to be in the FL commandlet.

For some reason $netAdapterList | fl Name, DeviceName, Guid, SharingEnabled did not work for me, so I ended up using the following instead:

$formatted = $netAdapterListParam | fl Name, DeviceName, Guid, SharingEnabled | Out-String
Write-Host $formatted

That did the trick and the 4 properties were printed on the screen.

Lessons learned:

1) The Powershell IDE built into Win7 can be a very useful tool for debugging scripts 2) Format-List can be quirky when formatting custom objects, so Out-String was required.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top