Formating output

This topic contains 12 replies, has 4 voices, and was last updated by Profile photo of Christian Bitudio Christian Bitudio 1 year, 8 months ago.

  • Author
    Posts
  • #17439
    Profile photo of fair64
    fair64
    Participant

    I am building a simple form that returns some AD info. The output is returned as an array @{InfrastructureMaster=dc1; PDCEmulator=dc2; RIDMaster=dc3}
    I would like to format this data in a more readable fashion by removing the @{}

    The code I am using is this:
    $roles = Get-ADDomain -Server dc1 | select InfrastructureMaster, PDCEmulator, RIDMaster
    $listbox1.Items.Add($roles)

    Any help would be greatly appreciated, new to the game so if there is a better way to retrieve and display this data please let me know.
    Thanks

  • #17440
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That's not actually an array; it's how PSObjects are displayed as strings (with similar syntax to a hashtable literal). You can easily produce any string format you want, but you need to know what the representation should be before you can write the code.

    Here's one simple way of doing it:

    $listbox1.Items.Add(($roles | Out-String))

    By using Out-String instead of just allowing $roles to be cast to a string, you go through PowerShell's formatting cmdlets. It produces the same text that you'd see at the console if you just typed "$roles".

  • #17441
    Profile photo of fair64
    fair64
    Participant

    I would like to thank you Dave for explaining this to me, on another forum I was told to learn powershell 1st before asking questions like this...
    I implemented the changes but the results I receive in the listbox are just the headers , no dc info. Playing around i tested the output in a richtextbox and that displays all the data correctly. Any reason why the listbox is only displaying the headers InfrastructureMaster, PDCEmulator, RIDMaster?

    Thanks Again in advance..

  • #17442
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I suspect that the ListBox is only displaying a single line of text, and the Out-String approach produces a table across multiple lines. There are ways around that as well (such as using a text box, as you discovered); just depends on what your needs are.

    I don't play with GUIs very much, and I need to fiddle with them quite a bit to get them working as well.

    "on another forum I was told to learn powershell 1st before asking questions like this..." Let me guess: did that comment come from someone named jrv on the TechNet forums? He says that to people quite often, and it annoys me as well.

  • #17443
    Profile photo of fair64
    fair64
    Participant

    Thanks Dave greatly appreciated your input It was on the Sapien Forum I would guess its the same guy his tag is jvierra. I try to ignore those type of people, Im just trying to learn and have fun with it...

    Thanks

  • #17444
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Yep, same guy. Frustrating, but what can ya do?

  • #17445
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Can you explain how you would like the list box to look? For instance, if you wanted something like this:

    $roles | foreach{$_.PSObject.Properties | foreach{"{0} ({1})" -f $_.Value, $_.Name}}
    
    server1.yourdomain.com (InfrastructureMaster)
    server4.yourdomain.com (PDCEmulator)
    server3.yourdomain.com (RIDMaster)
    

    then you would just do:

    $roles | foreach{$_.PSObject.Properties | foreach{$listbox1.Items.Add(("{0} ({1})" -f $_.Value, $_.Name))}}

    if you only want the server names, then you can just use $_.Value versus

    $listbox1.Items.Add($roles.InfrastructureMaster)
    $listbox1.Items.Add($roles.PDCEmulator)
    $listbox1.Items.Add($roles.RIDMaster)
    
    

    I haven't done alot with Powershell GUI either, but you could also look at using a PSObject as a datasource. There should be a method like $listbox1.Source($roles). Just wanted to give you options.

  • #17446
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    I used to go back and forth with jvierra all of the time on http://www.visualbasicscript.com. He can be very abrupt, but he is a very intelligent person and I learned a lot about coding from him. Just have to get past the rough exterior and concentrate on the code.

  • #17447
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I know. It just pisses me off when his attitude is the first thing a forum newcomer sees, and they wind up leaving because of it. I wouldn't put up with that kind of thing on PowerShell.org, but as it turns out, everyone seems to be pretty polite over here anyway. 🙂

  • #17448
    Profile photo of fair64
    fair64
    Participant

    Thanks Rob , that was the ticket. appreciate yours and Dave's responses.
    It is very evident that he has a wealth of knowledge, just a shame that he makes it such a volatile environment when people are trying to learn.
    Thanks again guys..

  • #17449
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You could always do a Magic! parity on Rude...


    Why you gotta be so rude
    I'm just not as technical dudeee
    Why you gotta be sooo ruudddee...
    Post that thread
    Post it anyway
    Post that thread
    Post it anyway
    I'm gonna post here anyway....yeaahhh

  • #17450
    Profile photo of fair64
    fair64
    Participant

    From now on this is were I will be hanging out 😉
    Great environment.

  • #23963
    Profile photo of Christian Bitudio
    Christian Bitudio
    Participant

    Hi. I'm new to powershell and I was encountering the same scenario where I could not get the output format that I needed. This thread really helped big time. I'm glad I joined this org.

You must be logged in to reply to this topic.