Author Posts

July 24, 2014 at 5:31 am

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

July 24, 2014 at 5:34 am

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

July 24, 2014 at 5:51 am

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

July 24, 2014 at 5:55 am

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.

July 24, 2014 at 6:04 am

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

July 24, 2014 at 6:16 am

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

July 24, 2014 at 6:16 am

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.

July 24, 2014 at 6:19 am

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.

July 24, 2014 at 6:23 am

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

July 24, 2014 at 6:27 am

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

July 24, 2014 at 6:39 am

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

July 24, 2014 at 6:48 am

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

April 5, 2015 at 3:47 am

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.