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
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
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
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.
Many thanks Luc
Phil
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
OK. Sounds reasonable.
I'll have a look at that doc when I get a little bit of time.
Thanks again Luc