Solved

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

Posted on 2014-09-30
6
197 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
  • 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 69

Assisted Solution

by:Qlemo
Qlemo earned 500 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
Is Your AD Toolbox Looking More Like a Toybox?

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.

 

Author Comment

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell query 2 23
Help needed with Powershell  XML to MySQL 5 37
Powershell XML in variable 4 20
Recycle MSExchnge powershell app Pool 2 16
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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). …

786 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