FillDeeUK
Contributor
Contributor

Can any help or explain this strange behaviour ?

Jump to solution

Hi Guys,

I am just starting to write a script to extract IP information from our VM hosts prior to upgrades.

I have ran into a strange error that I cannot get around, or explain and was wondering if anyone could help me.

This script (pass vmhost objects to it) results in showing the Physical nic info (device and mac), but no vnic info (device only) . If I change the order arounf so the vnic info is displayed first, then the vnic info deisplays correctly (device, IP address and Mask) but the pnic info doesn't (only displays device, not mac).

Begin

{}

Process

{

if

($_) {

$_.name

$temp = $_ | get-view

Write-Host " "

Write-Host "Physical Nics "

$temp.Config.Network.Pnic | select Device, mac

Write-Host " "

write-host "Virtual nics"

$temp.Config.Network.Vnic | select device, {$_.spec.ip.ipaddress}, {$_.spec.ip.subnetmask}

Write-Host " "

}

}

I know there are many scripts that do this, so please don't point me to them. I am trying to learn. Also, I know the appearence is ropey, I'll tidy that later. Right now, I'm only after the resaons for the strange behaviour.

Thanks in advance

Phil

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You are confusing the output formatter by sending it different types of objects.

The formatter looks at the first object to determine how to format it for screen output.

Try adding the Out-Default cmdlet after both of the Select-Object cmdlets you have in the function.

$temp.Config.Network.Pnic | select Device, mac | Out-Default

and

$temp.Config.Network.Vnic | select device, {$_.spec.ip.ipaddress}, {$_.spec.ip.subnetmask} | Out-Default

The Out-Default cmdlet in fact kind of resets the output formatter.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership

You are confusing the output formatter by sending it different types of objects.

The formatter looks at the first object to determine how to format it for screen output.

Try adding the Out-Default cmdlet after both of the Select-Object cmdlets you have in the function.

$temp.Config.Network.Pnic | select Device, mac | Out-Default

and

$temp.Config.Network.Vnic | select device, {$_.spec.ip.ipaddress}, {$_.spec.ip.subnetmask} | Out-Default

The Out-Default cmdlet in fact kind of resets the output formatter.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
FillDeeUK
Contributor
Contributor

Wow how fast was that answer !

That sorted it. Not quite sure that I fully understand the difference, as I thought the output went to out-default if it had nowhere else to send it, so not sure why adding it quite fixes the issue, but hey. Who cares, ti works. Smiley Happy

Many thanks Luc

Phil

0 Kudos
LucD
Leadership
Leadership

Have a look at Jeffrey Snover's post, called How PowerShell Formatting and Outputting REALLY works, as it explains in much more detail what happened.

Towards the middle of that post there is a paragraph that states "Now, if there is not a registered view for a datatype, then Out-Default looks at the FIRST OBJECT IN THE STREAM to determine how many properties the object has...".

That in fact explains what you saw.

You put 2 unknown types of objects, with the 2 Select cmdlets, on the pipeline.

And Out-Default looked at the first object and determined from that one what and how to display the data.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

FillDeeUK
Contributor
Contributor

OK. Sounds reasonable.

I'll have a look at that doc when I get a little bit of time.


Thanks again Luc

0 Kudos