Create list box items using outpuf from another script?

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of Stiks Stiks 8 months, 3 weeks ago.

  • Author
    Posts
  • #10410
    Profile photo of notarat
    notarat
    Participant

    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?

  • #10411
    Profile photo of notarat
    notarat
    Participant

    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.

  • #10412
    Profile photo of notarat
    notarat
    Participant

    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)

  • #10414
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    try this

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

  • #10415
    Profile photo of notarat
    notarat
    Participant

    [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

  • #10416
    Profile photo of notarat
    notarat
    Participant

    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!

  • #10421
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    just out of interest how are you getting the DCs?

  • #10443
    Profile photo of notarat
    notarat
    Participant

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

  • #54798
    Profile photo of Stiks
    Stiks
    Participant

    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

  • #54818
    Profile photo of Dan Potter
    Dan Potter
    Participant

    $listbox1.Items.AddRange($dcs)

  • #54863
    Profile photo of Stiks
    Stiks
    Participant

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

    Thanks again

    • This reply was modified 8 months, 3 weeks ago by Profile photo of Stiks Stiks.

You must be logged in to reply to this topic.