locked
Passing things through the pipeline and retaining information RRS feed

  • Question

  • Hi,

    Pretty basic question here which I am sure has been asked and ansered a thousand times, but I don't know what to search for to get an answer.

    If you have a command that gets a bunch of things, for example get-distributiongroup which grabs all Exchange distribution groups. You can then pipe that though to get-distributoingroupmember to show who has permission to each group.

    However, when you output that command (get-distributiongroup | get-distributiongroupmember) to screen or file, it just lists loads of users without saying what they are members of.

    This makes sense as I have worked with Powershell for years. You are taking the results of one command and firing them at the next command in the pipe, I get this. I know it's by design. I just wanted to clarify this point before any hlpful people spend time explaining what I already know.

    However, what I would like to know is how would you fix this? For example, for the above command. How would you get it to output the name of the group and the members in as simple way as possible.

    What i did was write a script and set up variables etc. to acheive this. But that's a pain to do, you end up writing a while little script out instead of a 4 word command. Maybe that's the only way, but i would like to know for sure that I am not missing a trick.

    Many thanks - James

    Monday, November 6, 2017 10:29 AM

Answers

  • Please lookin the Gallery for many scripts that do what you ask.  The reason you are asking such a beginner question is because, although you may have dabbled with PowerShell for "years", you have never taken the time to learn the fundamentals of PowerShell.  If you stop now and take a course or study a book on PowerShell you will quickly find the information you are mossing.

    PowerShell cannot be learned correctly by guesswork or by copying content fro the Internet.  If you are not a trained programmer then most advanced use of PowerShell will be beyond you understanding until you actually study PowerShell.

    This is normal for PowerShell. PS is designed to be usable at the prompt.  It can do simple execution of commands without much training. TO understand how to program with PS you need to learn programming with PowerShell.

    There are many features of PowerShell that allow us to leverage the pipeline to accomplish you required results. THe easiest one is to use the "PipeLineVariable" to capture and display the current object produced by any previous CmdLet.

    Get-DistributionGroup -PipelineVariable grp |   ... other CmdLets .... | Select @{n='GroupName';e={$grp.Name}}

    As you can see this allows us to refer to the results of a previous commands results.

    help about_CommonParameters

    Taking a forum course or studying a book would have helped you to know these things.  Now that you have significant exposure the training would be even more useful.


    \_(ツ)_/


    • Edited by jrv Monday, November 6, 2017 2:22 PM
    • Marked as answer by Jaymz 6 Monday, November 6, 2017 5:39 PM
    Monday, November 6, 2017 2:22 PM

All replies

  • First of all, there is no error to fix, this is simply what the code you have supplied will do.

    I would suggest passing the results of get-distributiongroup into a foreach loop which will get the group name and run a query for all the group members. I'm guessing that since you have year of experience with PowerShell that you should be able to work through the code for this. If you need some help please don't hesitate to ask :)

    Something like this?

    $groups = get-distributiongroup
    
    foreach ($I in $groups){
    
    write-host "group name is $I"
    $users = $i | get-distributiongroupmembers
    write-host "$users"
    }



    Monday, November 6, 2017 10:41 AM
  • Lol

    Me: "This makes sense as I have worked with Powershell for years. You are taking the results of one command and firing them at the next command in the pipe, I get this. I know it's by design. I just wanted to clarify this point before any hlpful people spend time explaining what I already know.

    What i did was write a script and set up variables etc. to acheive this."

    ITD: "First of all, there is no error to fix, this is simply what the code you have supplied will do."

    -

    As I said, I know how to sort this by writing a little script to do just that. What you have put is basically what I did. My question is not how I fix this with a script. But if there is another clever way to acheive this.

    There may not be another way short of writting a little script. Which is fine if that's how it is. But I am looking for confirmation or other ideas.

    But you have not answered my question - I think you missed the point of my question and answered something else.

    P.S. your script is wrong I believe (just incase google leads anyone here down the line).

    $users = $groups | get-distributiongroupmembers

    should be

    $users = $l | get-distributiongroupmembers

    Thanks for trying anyways ITD.

    Monday, November 6, 2017 12:12 PM
  • Hi James,

    I have fixed the code so that it is correct, thanks for pointing it out :)

    Sorry if I did exactly what your question warned me I shouldn't do but if you know PowerShell, probably better than me, you would know that this is pretty much the cleanest and simplest way of doing it. I cannot think of another way to run through all the results. Maybe someone else will have better idea.

    I'd trying Googling for the best way to sort through results in PowerShell. Maybe that will bring something new and exciting to the surface.

    Let us know how you get on!

    Monday, November 6, 2017 12:34 PM
  • Cheers ITD.

    I have googled this a few times, don't get any hits close to what I am looking for.

    Maybe it is not possible. Maybe it's something so simple I can't believe I missed it.

    If I find anything, I will report back here.

    Thanks

    Monday, November 6, 2017 12:55 PM
  • Please lookin the Gallery for many scripts that do what you ask.  The reason you are asking such a beginner question is because, although you may have dabbled with PowerShell for "years", you have never taken the time to learn the fundamentals of PowerShell.  If you stop now and take a course or study a book on PowerShell you will quickly find the information you are mossing.

    PowerShell cannot be learned correctly by guesswork or by copying content fro the Internet.  If you are not a trained programmer then most advanced use of PowerShell will be beyond you understanding until you actually study PowerShell.

    This is normal for PowerShell. PS is designed to be usable at the prompt.  It can do simple execution of commands without much training. TO understand how to program with PS you need to learn programming with PowerShell.

    There are many features of PowerShell that allow us to leverage the pipeline to accomplish you required results. THe easiest one is to use the "PipeLineVariable" to capture and display the current object produced by any previous CmdLet.

    Get-DistributionGroup -PipelineVariable grp |   ... other CmdLets .... | Select @{n='GroupName';e={$grp.Name}}

    As you can see this allows us to refer to the results of a previous commands results.

    help about_CommonParameters

    Taking a forum course or studying a book would have helped you to know these things.  Now that you have significant exposure the training would be even more useful.


    \_(ツ)_/


    • Edited by jrv Monday, November 6, 2017 2:22 PM
    • Marked as answer by Jaymz 6 Monday, November 6, 2017 5:39 PM
    Monday, November 6, 2017 2:22 PM
  • Nice thanks - that is what I was looking for.

    I have done about 30+ hours of training in Powershell over the years and none of it covered that. It all went from explaing how the pipeline works though to writing scripts, skipping that bit.

    From my initial investigations just now, it seems that this was only introduced in Powershell V4 (so anyone finding this page, this will be native with Server 2012 R2 and Win 8.1 and above). I expect all the training materials I had seen were pre V4 or at least in the early days of V4 before the people doing said training material had got the hang of this function.

    Anyway, that looks exactly what i was looking for. Ta

    Monday, November 6, 2017 5:38 PM
  • What most if not all video and online courses fail to teach is how to move forward with PowerShell. Each new version comes with pretty comprehensive release notes.  I always grab that first a nd sit down in the evening with a pint and some snacks and carefully review the release notes.  This is how we learn about new features.  I also grab the latest  book on each version and spend similar time reviewing all looking for anything I may have missed and for all changes, additions and feature updates.

    I and most programmers and techs I know do this with all subsystems and tools we use.  It help to save a lot of time and pre[pares us for new projects and requirements.  Just as a doctor must continuously seek re-education, a tech must retrain constantly.

    For current PS I recommend the latest book, PowerShell in Action by Bruce Payette and Richard Siddaway  It is one of the most complete manuals and references yet published.

    Actually the method of capturing the pipeline variable was always available but not as a parameter.  Without the parameter we would be forced to use a "ForEach-Object" to combine or, PS2 and later, calculated properties. The parameter makes this more flexible. The new method allows us to declare variable scoped to the whole pipeline without defining them at a script or global scope.

    Happy PosHing


    \_(ツ)_/

    Monday, November 6, 2017 5:56 PM