notarat
September 27, 2013, 5:08am
1
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?
notarat
September 27, 2013, 5:37am
2
Okay…I guess it is possible, but it looks bad…
What it should look like is:
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.
notarat
September 27, 2013, 5:49am
3
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)
try this
$DCs=(code to get all domain controllers goes here)
foreach($DC in $DCs) {
[void] $objListBox.Items.Add(“$($DC)”)
}
notarat
September 27, 2013, 6:46am
5
Thanks for the response!
The suggested change doesn’t seem to be displaying them any differently in the selection list…weird
notarat
September 27, 2013, 6:59am
6
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!
just out of interest how are you getting the DCs?
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.
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
$listbox1.Items.AddRange($dcs)
Thanks so much, that works perfectly and found the value - $objlistbox.selecteditem
Thanks again