Author Posts

September 27, 2013 at 10:38 am

List box creation is currently a manual process consisting of manually entering the following code to show each DC from which the user can select:

[void] $objListBox.Items.Add("atl-dc-001")
[void] $objListBox.Items.Add("atl-dc-002")
[void] $objListBox.Items.Add("atl-dc-003")
[void] $objListBox.Items.Add("atl-dc-004")
[void] $objListBox.Items.Add("atl-dc-005")
[void] $objListBox.Items.Add("atl-dc-006")
[void] $objListBox.Items.Add("atl-dc-007")

What I want to do is to build that list above dynamically, based on the results of another script so that if "atl-dc-004" is offline, the list does not offer it as a selectable item

If dc-004 is offline, the list would look like:

[void] $objListBox.Items.Add("atl-dc-001")
[void] $objListBox.Items.Add("atl-dc-002")
[void] $objListBox.Items.Add("atl-dc-003")
[void] $objListBox.Items.Add("atl-dc-004")
[void] $objListBox.Items.Add("atl-dc-005")
[void] $objListBox.Items.Add("atl-dc-006")
[void] $objListBox.Items.Add("atl-dc-007")

If we added a new DC the list would look like

[void] $objListBox.Items.Add("atl-dc-001")
[void] $objListBox.Items.Add("atl-dc-002")
[void] $objListBox.Items.Add("atl-dc-003")
[void] $objListBox.Items.Add("atl-dc-004")
[void] $objListBox.Items.Add("atl-dc-005")
[void] $objListBox.Items.Add("atl-dc-006")
[void] $objListBox.Items.Add("atl-dc-007")
[void] $objListBox.Items.Add("atl-dc-008")

Basically I want to dynamically create the choosable items on the list.

What I was thinking is that I can use some sort of foreach like

$DCs=(code to get all domain controllers goes here)
foreach($DC in $DCs) {
    [void] $objListBox.Items.Add("$DC")
    }

Is that possible?

September 27, 2013 at 11:07 am

Okay...I guess it is possible, but it looks bad...

What it should look like is:
Picture of item list to choose from

What it looks like is

@{name=atl-dc-001}
@{name=atl-dc-002}
.
.
.
@{name=atl-dc-006}
@{name=atl-dc-007}

Which looks pretty nasty.

September 27, 2013 at 11:19 am

Looks like I need to remove the @{name= and the } somehow (I hate this part because split/replace confuse me almost as much as foreach and for each, lol)

September 27, 2013 at 11:59 am

try this

$DCs=(code to get all domain controllers goes here)
foreach($DC in $DCs) {
[void] $objListBox.Items.Add("$($DC)")
}

September 27, 2013 at 12:16 pm

[quote=10414]try this

$DCs=(code to get all domain controllers goes here)
foreach($DC in $DCs) {
[void] $objListBox.Items.Add("$($DC)")
}

[/quote]

Thanks for the response!

The suggested change doesn't seem to be displaying them any differently in the selection list...weird

September 27, 2013 at 12:29 pm

I changed it slightly to

foreach($DC in $DCs) { 
[void] $objListBox.Items.Add("$($DC.name)")
}

That seems to be working.

Thanks for the assistance! I would not have gotten this far without your help!

September 29, 2013 at 2:36 am

just out of interest how are you getting the DCs?

October 1, 2013 at 5:42 am

[quote=10421]just out of interest how are you getting the DCs?[/quote]

I'd paste the code in that shows how I got the DC information but I am on Furlough so I don't have access to my code repository. Sorry.

October 3, 2016 at 3:33 am

Hi, I am trying to use data retrieved from a invoke-sqlcmd to populate the list box; similar to what you have above (within a foreach.

I have tried –

foreach ($CardCode in $Cardcodes) {

[void] $objListBox.Items.Add($($cardcodes.cardcode))
& also tried
[void] $objListBox.Items.Add($("$cardcodes.cardcode)")

}

But the list box is resolving as the object , not the values within
They show as "System.Object[]"
I have tried many options but cannot seem to resolve this.

Any help would be appreciates
Thanks in advance

October 3, 2016 at 2:34 pm

$listbox1.Items.AddRange($dcs)

October 3, 2016 at 10:14 pm

Thanks so much, that works perfectly and found the value – $objlistbox.selecteditem

Thanks again

  • This reply was modified 1 year, 11 months ago by  Stiks.