Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Need to slim down the output of a for loop

Posted on 2013-01-23
Medium Priority
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>
Question by:Thaidog
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

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


Accepted Solution

coraxal earned 2000 total points
ID: 38813069
Actually you need to enclose the Select-Object cmdlet inside the scriptblock for the output to produce "parent" 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"

Author Comment

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

LVL 40

Expert Comment

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

LVL 40

Expert Comment

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..

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

715 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