Solved

Need to slim down the output of a for loop

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

776 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