$ComboBox add a hidden ID

SJB 1 Reputation point
2021-08-19T16:31:57.917+00:00

I am making a GUI for some partner center powershell commands. So far I have it losting all our customers names in a combobox but I then need to get the CustomerID for the next stage but all I can return is their name.

I would like to add a property for the ID and then be able to retrieve it when a customer is selected. My code is below but I can't find out how to add $Customer.CustomerID to the item

$ComboBox = New-Object System.Windows.Forms.ComboBox
$ComboBox.Width = 300
$Customers = Get-PartnerCustomer
Foreach ($Customer in $Customers)
{
$ComboBox.Items.Add($Customer.Name);

}
$ComboBox.Location  = New-Object System.Drawing.Point(80,10)
$main_form.Controls.Add($ComboBox)

$Label2 = New-Object System.Windows.Forms.Label

$Label2.Text = “Users”
$Label2.Location  = New-Object System.Drawing.Point(10,60)
$Label2.AutoSize = $true
$main_form.Controls.Add($Label2)


$Label3 = New-Object System.Windows.Forms.Label
$Label3.Text = “”
$Label3.Location  = New-Object System.Drawing.Point(10,90)
$Label3.AutoSize = $true
$main_form.Controls.Add($Label3)

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,10)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = “Get Users”
$main_form.Controls.Add($Button)

$Button.Add_Click(
{
$Label3.Text = $ComboBox.selectedItem
)

$main_form.ShowDialog()
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 32,246 Reputation points
    2021-08-23T13:42:21.053+00:00

    How are you building the $Customers variable? A csv? Can you just do a match?

    cls  
    $Customers = import-csv c:\temp\foo.csv  
    $Customers  
    "Now do a Lookup"  
    $LookFor = "YYYY"  
    $SelectedCust = $Customers -match $LookFor   
    $SelectedCust.Id  
    "Or this way"  
    $LookFor = "XXXX"  
    $SelectedCust = $Customers.Where({$PSItem.Name -eq $LookFor})  
    $SelectedCust.Id  
      
    

    125666-capture.jpg

    https://devblogs.microsoft.com/scripting/powertip-get-row-from-csv-file-based-on-value/

    0 comments No comments