Solved

Need to slim down the output of a for loop

Posted on 2013-01-23
6
435 Views
Last Modified: 2013-04-10
How do I get the output of the for loop to just give me the parent field below? I tried appending select-object but it did not seem to work...

PS C:\Users\> foreach ($MAC in $VM) { Get-VM | Get-NetworkAdapter | Where-Object {$_.MacAddress -eq "$MAC"} | fl

}

 
MacAddress             : <mac address removed>

WakeOnLanEnabled : True

NetworkName           : <network name removed>

Type                         : e1000

ParentId                 : VirtualMachine-vm-74856

Parent                     : <name removed

Uid                           : /VIServer=domainname\mysuername@mydomain004:443/VirtualMachine=VirtualMachine-vm-74856/NetworkAdapter=4000/

ConnectionState   : NotConnected, GuestControl, StartConnected

ExtensionData       : VMware.Vim.VirtualE1000

Id                             : VirtualMachine-vm-74856/4000

Name                         : Network adapter 1

Open in new window


<Addresses removed by AnnieMod, EE Admin>
0
Comment
Question by:Thaidog
  • 2
  • 2
6 Comments
 
LVL 5

Expert Comment

by:coraxal
ID: 38812794
Try using the Select-Object
foreach ($MAC in $VM) { Get-VM | Get-NetworkAdapter | Where-Object {$_.MacAddress -eq "$MAC"}} | Select Parent

Open in new window

0
 
LVL 5

Accepted Solution

by:
coraxal earned 500 total points
ID: 38813069
Actually you need to enclose the Select-Object cmdlet inside the scriptblock for the output to produce "parent"...like this:
foreach ($MAC in $VM) { Get-VM | Get-NetworkAdapter | Where-Object {$_.MacAddress -eq "$MAC"} | Select-Object Parent }

Open in new window


Otherwise the output is going to return other properties along with "parent"
0
 

Author Comment

by:Thaidog
ID: 38813823
Not working:

An empty pipe element is not allowed.
At line:1 char:97
+ foreach ($MAC in $VM) { Get-VM | Get-NetworkAdapter | Where-Object {$_.MacAddress -eq "$MAC"}} | <<<<  Select Parent
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38813830
Try the second command posted by coraxal..Select should be inside the foreach loop..

foreach ($MAC in $VM) { Get-VM | Get-NetworkAdapter | Where-Object {$_.MacAddress -eq "$MAC"} | Select-Object Parent }

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38813843
coraxal had given the correct answer and he deserve this points.. :-)

I have requested MOD to reopen the question, so you can reassign the points..
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now