none
Why does Get-ADObject not work in this script? RRS feed

  • Question

  • Ok, I've created a form using primalforms (something blogged about on the scripting guys last year) so a coworker will stop complaining that my search and restore deleted accounts script is command line (even though I created it as a menu and made it as simple for him as possible). The problem is, the Search function complains that it doesn't like Get-ADObject or -Filter. the code is rather long (primalforms standard) but is listed below.

    #Generated Form Function
    function GenerateForm {
    ########################################################################
    # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
    # Generated On: 2/3/2014 3:33 PM
    # Generated By: gdavies
    ########################################################################
    
    #region Import the Assemblies
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    #endregion
    
    #region Generated Form Objects
    $ADSearchandRestore = New-Object System.Windows.Forms.Form
    $Results = New-Object System.Windows.Forms.TextBox
    $groupBox1 = New-Object System.Windows.Forms.GroupBox
    $Resultslabel = New-Object System.Windows.Forms.Label
    $Instructions = New-Object System.Windows.Forms.Label
    $Input = New-Object System.Windows.Forms.TextBox
    $Exit = New-Object System.Windows.Forms.Button
    $Restore = New-Object System.Windows.Forms.Button
    $Search = New-Object System.Windows.Forms.Button
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    #endregion Generated Form Objects
    
    #----------------------------------------------
    #Generated Event Script Blocks
    #----------------------------------------------
    #Provide Custom Code for events specified in PrimalForms.
    $Restore_OnClick= 
    {
    #TODO: Place custom script here
    
    }
    
    $Exit_OnClick= 
    {$ADSearchandRestore.Close()}
    
    $loadmodule= 
    {import-module activedirectory
    set-location ad:}
    
    $handler_label2_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $Search_OnClick={
         $results.Text=Get-ADObject -Filter "displayName -eq '$($input.text)'" -IncludeDeletedObjects | Out-String
         $Results.Focus()
    }
    
    $OnLoadForm_StateCorrection=
    {#Correct the initial state of the form to prevent the .Net maximized form issue
    	$ADSearchandRestore.WindowState = $InitialFormWindowState
    }
    
    
    #----------------------------------------------
    #region Generated Form Code
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 249
    $System_Drawing_Size.Width = 289
    $ADSearchandRestore.ClientSize = $System_Drawing_Size
    $ADSearchandRestore.DataBindings.DefaultDataSourceUpdateMode = 0
    $ADSearchandRestore.Name = "ADSearchandRestore"
    $ADSearchandRestore.Text = "AD Search & Restore"
    $ADSearchandRestore.add_Load($loadmodule)
    
    $Results.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 145
    $Results.Location = $System_Drawing_Point
    $Results.Name = "Results"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 20
    $System_Drawing_Size.Width = 264
    $Results.Size = $System_Drawing_Size
    $Results.TabIndex = 8
    $Results.add_TextChanged($handler_textBox2_TextChanged)
    
    $ADSearchandRestore.Controls.Add($Results)
    
    
    $groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 116
    $groupBox1.Location = $System_Drawing_Point
    $groupBox1.Name = "groupBox1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 5
    $System_Drawing_Size.Width = 264
    $groupBox1.Size = $System_Drawing_Size
    $groupBox1.TabIndex = 7
    $groupBox1.TabStop = $False
    
    $ADSearchandRestore.Controls.Add($groupBox1)
    
    $Resultslabel.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 11
    $System_Drawing_Point.Y = 132
    $Resultslabel.Location = $System_Drawing_Point
    $Resultslabel.Name = "Resultslabel"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 265
    $Resultslabel.Size = $System_Drawing_Size
    $Resultslabel.TabIndex = 6
    $Resultslabel.Text = "Results"
    $Resultslabel.add_Click($handler_label2_Click)
    
    $ADSearchandRestore.Controls.Add($Resultslabel)
    
    $Instructions.DataBindings.DefaultDataSourceUpdateMode = 0
    $Instructions.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9,0,3,0)
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 9
    $Instructions.Location = $System_Drawing_Point
    $Instructions.Name = "Instructions"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 265
    $Instructions.Size = $System_Drawing_Size
    $Instructions.TabIndex = 4
    $Instructions.Text = "Enter the full Display Name you wish to search for/restore in the box below"
    $Instructions.TextAlign = 32
    
    $ADSearchandRestore.Controls.Add($Instructions)
    
    $Input.DataBindings.DefaultDataSourceUpdateMode = 0
    $Input.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,0)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 61
    $Input.Location = $System_Drawing_Point
    $Input.Name = "Input"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 20
    $System_Drawing_Size.Width = 265
    $Input.Size = $System_Drawing_Size
    $Input.TabIndex = 3
    
    $ADSearchandRestore.Controls.Add($Input)
    
    
    $Exit.DataBindings.DefaultDataSourceUpdateMode = 0
    $Exit.FlatStyle = 1
    $Exit.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,0)
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 202
    $System_Drawing_Point.Y = 87
    $Exit.Location = $System_Drawing_Point
    $Exit.Name = "Exit"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 75
    $Exit.Size = $System_Drawing_Size
    $Exit.TabIndex = 2
    $Exit.Text = "Exit"
    $Exit.UseVisualStyleBackColor = $True
    $Exit.add_Click($Exit_OnClick)
    
    $ADSearchandRestore.Controls.Add($Exit)
    
    $Restore.BackColor = [System.Drawing.Color]::FromArgb(255,255,0,0)
    
    $Restore.DataBindings.DefaultDataSourceUpdateMode = 0
    $Restore.FlatStyle = 1
    $Restore.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,0)
    $Restore.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 108
    $System_Drawing_Point.Y = 87
    $Restore.Location = $System_Drawing_Point
    $Restore.Name = "Restore"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 75
    $Restore.Size = $System_Drawing_Size
    $Restore.TabIndex = 1
    $Restore.Text = "Restore"
    $Restore.UseVisualStyleBackColor = $False
    $Restore.add_Click($Restore_OnClick)
    
    $ADSearchandRestore.Controls.Add($Restore)
    
    $Search.BackColor = [System.Drawing.Color]::FromArgb(255,144,238,144)
    
    $Search.DataBindings.DefaultDataSourceUpdateMode = 0
    $Search.FlatStyle = 1
    $Search.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,0)
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 87
    $Search.Location = $System_Drawing_Point
    $Search.Name = "Search"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 75
    $Search.Size = $System_Drawing_Size
    $Search.TabIndex = 0
    $Search.Text = "Search"
    $Search.UseVisualStyleBackColor = $False
    $Search.add_Click($Search_OnClick)
    
    $ADSearchandRestore.Controls.Add($Search)
    
    #endregion Generated Form Code
    
    #Save the initial state of the form
    $InitialFormWindowState = $ADSearchandRestore.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $ADSearchandRestore.add_Load($OnLoadForm_StateCorrection)
    #Show the Form
    $ADSearchandRestore.ShowDialog()| Out-Null
    
    } #End Function
    
    #Call the Function
    GenerateForm

    The code associated with the Search button is in lines 50 to 53PS

    This opens the form, and the Exit button works (yay), but entering a display name and hitting search gives the error belowD:\> \zzUntitled14.ps1


    Get-ADObject : The search filter cannot be recognized

    At H:\zzUntitled14.ps1:51 char:20

    +      $results.Text=Get-ADObject -Filter "displayName -eq '$($input.text)'" -Incl ...

    +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [Get-ADObject], ADException

        + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADObject

    Can anyone tell me what is wrong here?

    for info, $input.text is the text box entry on the form, the user types in the name they want to search for here. $results.text is the output, so it should display the results of the search here.

    The form looks like this

    I have, as yet, not tried to add any code for the restore button, I want to get the search working first.


    • Edited by GADavies Tuesday, February 4, 2014 4:27 PM correct mistake in question
    Tuesday, February 4, 2014 4:25 PM

Answers

  • I solved it. The reaon it doesn't work is $input is a reserved variable, I changed the variable name to $object and it started working. 
    • Marked as answer by GADavies Tuesday, February 4, 2014 6:08 PM
    Tuesday, February 4, 2014 6:08 PM

All replies

  • I solved it. The reaon it doesn't work is $input is a reserved variable, I changed the variable name to $object and it started working. 
    • Marked as answer by GADavies Tuesday, February 4, 2014 6:08 PM
    Tuesday, February 4, 2014 6:08 PM
  • I solved it. The reaon it doesn't work is $input is a reserved variable, I changed the variable name to $object and it started working. 

    That answer was recently posted on the Sapien forums for GUI forms.

    As I noted before.  Use good names for variables and stay away from simple names like $args, $inputm $PSObject, $PS* (anything beginning with $PS), $input, $error, $object and numerous other simple names.  Assume if it is too simple of  areference like $input and $output that it may actually a reserved word or may be used globally by another block of code.

    With Forms controls use real names like $DisplayName.  I porefer using control Identifiers to further separate the names like $tbDisplayName.  This actually works well with intelli-sense type systems.  We can also reverse that as some do to $DisplayName_tb or $DisplayName_textbox.

    In PowerShell, if in doubt, do this $_input.  The leading underscore is not used globally except is some custom modules.

    HELP about-Reserved_Words

    This will not list the variables in use and some vars are hidden

    Here is a partial list of the V2 reserved variables: http://stam.blogs.com/8bits/2010/05/variables-in-powershell-.html

    A fresh start with Powershell and type GV will list all predefined variables.  You should try to never assign to them or redefine them.


    ¯\_(ツ)_/¯

    Tuesday, February 4, 2014 6:57 PM