locked
Selecting Items as variables RRS feed

  • Question

  • I am, for the first time, trying to create a popup selectable item list in powershell. Can someone help me understand why this code is not defining the $Printer1 variable?

    #Extract Printer list from Active Directory
    $Printerlike = $AutoLogonAccountName -replace ".{5}$" #drop the last 5 characters
    $PrinterList = (Get-ADGroup -Filter * | Where {$_.name -like "ptr.gs.$Printerlike*"}).name
    
    #Prompt for Primary Printer Selection
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    
    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Select a Primary Printer"
    $objForm.Size = New-Object System.Drawing.Size(300,200) 
    $objForm.StartPosition = "CenterScreen"
    
    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$Printer1=$objListBox.SelectedItem;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$objForm.Close()}})
    
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$Printer1=$objListBox.SelectedItem;$objForm.Close()})
    $objForm.Controls.Add($OKButton)
    
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)
    
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20) 
    $objLabel.Size = New-Object System.Drawing.Size(280,20) 
    $objLabel.Text = "Please select a Default Printer:"
    $objForm.Controls.Add($objLabel) 
    
    $objListBox = New-Object System.Windows.Forms.ListBox 
    $objListBox.Location = New-Object System.Drawing.Size(10,40) 
    $objListBox.Size = New-Object System.Drawing.Size(260,20) 
    $objListBox.Height = 80
    
    $PrinterList | ForEach-Object {[void] $objListBox.Items.Add($_)}
    
    #[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")
    
    $objForm.Controls.Add($objListBox) 
    
    $objForm.Topmost = $True
    
    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()

    Wednesday, October 12, 2016 6:24 PM

Answers

  • {$script:Printer1=$objListBox.SelectedItem;$objForm.Close()}})


    \_(ツ)_/

    • Marked as answer by abetzold Wednesday, October 12, 2016 8:19 PM
    Wednesday, October 12, 2016 7:50 PM

All replies