Go Premium for a chance to win a PS4. Enter to Win


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

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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…
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

926 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