?
Solved

Powershell Access with FL to fields of the first command on the pipe

Posted on 2014-09-30
6
Medium Priority
?
207 Views
Last Modified: 2014-10-05
Imagine this PS :

Get-Mailbox "xxx"  --> one of the fields returned could be "emailaddresses"

ok , now I build a pipe like :

Get-mailbox "xxx" | Get-mailboxstatistics | FL emailaddresses, lastlogontime

where lastlogontime belongs to the Get-mailboxstatistics command and emailadresses belongs to Get-mailbox command.

Question is : How can I access the fields returned by the first command ? Apparently I can only diplay waht is returned by the last command on the pipe.

many thnks in advance !

** dont' try to find any sense on the command above .. just a way to illustrate the problem I have on a more complex issue.
0
Comment
Question by:josepmariasanchez
[X]
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
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 40351625
Usually pipelines are meant to only return a subset of data, so I would structure it like the following (using your example, not that it would make sense)

$Mailboxes = get-mailbox "xxx"
foreach ($Mailbox in $Mailboxes){
$Statistics = $Mailbox | get-mailboxstatistics | Select lastlogontime
"Mailbox info: $($Mailbox.emailaddress) Last Login: $Statistics"
}

Open in new window


As soon as you pass variables across, whatever is not kept won't be accessible.  I use the marathon example:

(Given ficticious results of $Runners = 1000)
Show all participants that were Male
$Runners | ?{$_.gender -eq "male"}
$Runners.count = 456
Show all remaining participants that were under 25  (out of a possible 456, not 1000)
$Runners | ?{$_.gender -eq "male"} | ?{$_.age -lt "25"}
$Runners.count = 120
Show all remaining participants that finished under 4 hours
$Runners | ?{$_.gender -eq "male"} | ?{$_.age -lt "25"} | ?{$_.time -lt "4.0"}
$Runners.count = 23

This is why it's not a good idea to put multiple cmdlets inside of a piped command since you lose everything like you've mentioned.  The only good method to  your solution (someone will correct me if I'm wrong) is to do a foreach on your initial to salvage the variable values.

Hope that helps,

Dale Harris
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 2000 total points
ID: 40351675
Dale, if we use a foreach statement with the result of a cmdlet, it is much better to use that cmdlet directly. Intermediate variables for huge data amounts are bad (in general)!
foreach ($Mailbox in get-mailbox "xxx"){
$Statistics = $Mailbox | get-mailboxstatistics | Select lastlogontime
"Mailbox info: $($Mailbox.emailaddress) Last Login: $Statistics"
}

Open in new window

The bad thing about that statement is that it does not return values you can use in a pipe (that easily). If in a pipe, we should use the foreach-object. I don't agree to "This is why it's not a good idea to put multiple cmdlets inside of a piped command", as we just need to store intermediate results into vars if needed.
get-mailbox "xxx" | % { $_ | Add-Member NoteProperty LastLogonTime (Get-mailboxstatistics $_ | select LastLogonTime) -PassThru }

Open in new window

for example adds a single property to the mailbox object.
0
 

Accepted Solution

by:
josepmariasanchez earned 0 total points
ID: 40351895
Hi There !  
The Add-Member gave me a clue on how to solve it :

$Mailboxes = Get-Mailbox
$Mailboxes | add-member  noteproperty  LastLogonTime2  Null
foreach ($Mailbox in $Mailboxes ) {$Mailbox.LastLogonTime2=(get-MailboxStatistics -identity  $Mailboxes.name | select lastlogontime)}
$Mailboxes
0
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!

 

Author Comment

by:josepmariasanchez
ID: 40351905
Thks a lot !
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40351917
That is a very complicated way to do it, and in no way to prefer over my suggestion. Try to keep away from storing objects in vars as much as possible. Objects are fat, and scripts with such a technique are memory hogs.
0
 

Author Closing Comment

by:josepmariasanchez
ID: 40361884
Hint given by previous post helped me with final solution
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

764 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