Solved

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

Posted on 2014-09-30
6
196 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 68

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
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

 

Author Comment

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

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

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now