Solved

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

Posted on 2014-09-30
6
194 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

757 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

21 Experts available now in Live!

Get 1:1 Help Now