Need to slim down the output of a for loop

Posted on 2013-01-23
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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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 …

752 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