locked
Listbox in powershell does not refresh after click on search button RRS feed

  • Question

  • Hello Guys,

    i started in learning Powershell and below is my first try of creating something usefull.

    My goal is to install automatically some printers which are listed in our AD. The Printer itself has Keywords and you can search with those Keywords for the Printer in the Search Field.

    After clicking on "Search" the ListBox should refresh with the search result - and now this is my problem - its not working, i tried different solutions but it does not work.

    When ill export the Result of $Search_Result ill get some printers which fit to the Keyword. So something is wrong in my foreach statement?

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    #Variables
    $Username = Get-ADUser $env:Username | Select-Object -ExpandProperty Name
    $Department = Get-Aduser $env:Username -Properties Department | Select-Object -ExpandProperty Department
    $Title = Get-Aduser $env:Username -Properties Title | Select-Object -ExpandProperty Title
    $Printers = Get-ADObject -LDAPFilter "(objectClass=printQueue)" -Searchbase ('DC=domain,dc=local') -Properties * | where-object{$_.keywords -contains "$Department"} | Select-Object Name 
    $Printers2 = $printers.tostring().split('-')
    $Font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Bold)
    
    
    #At click action
    $handler_Search_click= 
    {
      $Search_Result = Get-ADObject -LDAPFilter "(objectClass=printQueue)" -Searchbase ('DC=domain,dc=local') -Properties * | where-object{$_.keywords -contains $objTextbox1.text} | Select-Object -ExpandProperty Name 
      if ($Search_Result){
        $Search_Result > c:\Temp\Test.txt
        Foreach ($Search_Result in $Search_Results) 
        {
      [void] $objListBox.Items.Add($Search_Result."Name")
    
    }
    $objListBox.Refresh() 
    
    }
      Else {
      #export to Testfile
       $Search_Result > c:\Temp\Test.txt
    }
      }
    
    
    
    #Basic Gui Design
    $objForm = New-Object -TypeName System.Windows.Forms.Form
    $objForm.Text = 'Drucker Installation'
    $objForm.Font = $Font
    $objForm.Width = 800
    $objForm.Height = 600
    $objForm.BackColor ='#ffffff'
    $objForm.StartPosition ='CenterScreen'
    
    #Welcome Text for authenticated User
    $objlabel1 = New-Object System.Windows.Forms.Label
    $objlabel1.Location = New-Object System.Drawing.Size(10,5)
    $objlabel1.Size = New-Object System.Drawing.Size(420,40)
    $objlabel1.Text = "$Username               $Title           $Department "
    $objForm.Controls.Add($objlabel1)
    
    #Label Text - Choose your printer
    $objlabel3 = New-Object System.Windows.Forms.Label
    $objlabel3.Location = New-Object System.Drawing.Size(10,65)
    $objlabel3.Size = New-Object System.Drawing.Size(400,20)
    $objlabel3.Text = '1. '
    $objForm.Controls.Add($objlabel3)
    
    #Input Search Box
    $objTextBox1 = New-Object System.Windows.Forms.TextBox 
    $objTextBox1.Location = New-Object System.Drawing.Size(10,95) 
    $objTextBox1.Size = New-Object System.Drawing.Size(400,15) 
    $objForm.Controls.Add($objTextBox1) 
    
    # Add an Search button
    $objSearchButton = new-object System.Windows.Forms.Button
    $objSearchButton.Location = new-object System.Drawing.Size(10,125)
    $objSearchButton.Size = new-object System.Drawing.Size(75,25)
    $objSearchButton.Text = "Search"
    $objSearchButton.Add_Click($handler_Search_click)
    $objform.Controls.Add($objSearchButton)
    
    #Label Text - Choose your printer
    $objlabel2 = New-Object System.Windows.Forms.Label
    $objlabel2.Location = New-Object System.Drawing.Size(10,240)
    $objlabel2.Size = New-Object System.Drawing.Size(385,15)
    $objlabel2.Text = '2.Printer.'
    $objForm.Controls.Add($objlabel2)
    
    #Create new List Box
    $objListBox = New-Object System.Windows.Forms.ListBox 
    $objListBox.Location = New-Object System.Drawing.Size(10,260) 
    $objListBox.Size = New-Object System.Drawing.Size(400,300) 
    $objListBox.Height = 300
    
    #Add for each Entry a new Row
    Foreach ($Printer in $Printers) {
      [void] $objListBox.Items.Add($Printer."Name")
    }
    $objForm.Controls.Add($objListBox) 
    $objForm.Topmost = $True
    
    # Add an OK button
    $OKButton = new-object System.Windows.Forms.Button
    $OKButton.Location = new-object System.Drawing.Size(610,530)
    $OKButton.Size = new-object System.Drawing.Size(75,25)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$objForm.Close()})
    $objform.Controls.Add($OKButton)
    
    # Add an Exit button
    $ExitButton = New-Object System.Windows.Forms.Button
    $ExitButton.Location = New-Object System.Drawing.Size(700,530)
    $ExitButton.Size = New-Object System.Drawing.Size(75,25)
    $ExitButton.Text = "Exit"
    $objForm.CancelButton = $ExitButton
    $objForm.Controls.Add($ExitButton)
    
    
    
    
    [void] $objForm.ShowDialog()

    Thanks for your Help.

    Tuesday, September 19, 2017 9:58 AM

Answers

  • Simple issue.  Too much code that does nothing useful:

    #At click action
    $handler_Search_click = {
    	$objListBox.Items.Clear()
    	Get-ADObject -LDAPFilter '(objectClass=printQueue)' | 
    		where-object{ $_.keywords -contains $objTextbox1.text } |
    		ForEach-Object{[void]$objListBox.Items.Add($_.Name)	}
    }
    
    


    \_(ツ)_/

    • Marked as answer by BenTop Tuesday, September 19, 2017 11:35 AM
    Tuesday, September 19, 2017 10:17 AM

All replies

  • Simple issue.  Too much code that does nothing useful:

    #At click action
    $handler_Search_click = {
    	$objListBox.Items.Clear()
    	Get-ADObject -LDAPFilter '(objectClass=printQueue)' | 
    		where-object{ $_.keywords -contains $objTextbox1.text } |
    		ForEach-Object{[void]$objListBox.Items.Add($_.Name)	}
    }
    
    


    \_(ツ)_/

    • Marked as answer by BenTop Tuesday, September 19, 2017 11:35 AM
    Tuesday, September 19, 2017 10:17 AM
  • This should be the Click Action when i click on the Search Button.

    It looks for my AD Objects for the Keyword which is in the Textbox1 Field and should list for each result in the listbox in separate lines the printers.

    What would be a solution?

    Tuesday, September 19, 2017 10:31 AM
  • Run this at a prompt with the value that would be in the textbox.

    $text = 'what you are searching for'
    Get-ADObject -LDAPFilter '(objectClass=printQueue)' |
    	where-object{ $_.keywords -contains $text } |
    	ForEach-Object{ $_.Name }

    You have to be sure your basic code actually works as you expect.


    \_(ツ)_/


    • Edited by jrv Tuesday, September 19, 2017 10:35 AM
    Tuesday, September 19, 2017 10:34 AM
  • Run this at a prompt with the value that would be in the textbox.

    $text = 'what you are searching for'
    Get-ADObject -LDAPFilter '(objectClass=printQueue)' |
    	where-object{ $_.keywords -contains $text } |
    	ForEach-Object{ $_.Name }

    You have to be sure your basic code actually works as you expect.


    \_(ツ)_/


    It shows me only printers when ill use the -properties *. 

    After this ill get now: 

    Exception calling "Add" with "1" argument(s): "Der Wert darf nicht NULL sein.
    Parametername: item"
    At \\srvfs01\USERHOME$\ToparB\PS_MapPrinters.ps1:64 char:29
    +             ForEach-Object {[void]$objListBox.Items.Add($_.Name) }
    +                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentNullException


    After a few minutes of code reading the first answer from jrv worked. Thank you!

    • Edited by BenTop Tuesday, September 19, 2017 11:35 AM Answer worked!
    Tuesday, September 19, 2017 11:12 AM
  • No way to understand what you are doing.  The error did not come from the code I posted.  Did you even try  it?

    Why add "-Properties *" when you only want "Name". It serves no purpose.

    I think that you should learn PowerShell before attempting to use forms.

    Start here: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276<o:p></o:p>



    \_(ツ)_/

    Tuesday, September 19, 2017 5:12 PM