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
Solved

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

Posted on 2014-09-30
6
198 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Problems using Powershell and Active Directory?

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
A procedure for exporting installed hotfix details of remote computers using powershell
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

809 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