Page 1 of 2

Get-adobject doesn't work using GUI

Posted: Fri Jan 31, 2014 2:35 pm
by daviesg6
So, I used PrimalForms to create GUI for a coworker who doesn't like command line. I wrote him a nice menu based script but because it was still in the Powershell window he sulked.
The script, when not included in a form/GUI, searches AD, including deleted items for a user object. You then have the option to restore the object if required.

The powershell created by PrimalForms is below, my script additions are in the Generated Event Script Blocks section. I have not added anything for the restore button.
PowerShell Code
Double-click the code block to select all.
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 1/31/2014 3:18 PM
# Generated By: gdavies
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$ADSearchandRestore = New-Object System.Windows.Forms.Form
$Results = New-Object System.Windows.Forms.ListView
$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= 
{


}

$loadmodule= 
{import-module activedirectory
set-location ad:

}

$Exit_OnClick= 
{$ADSearchandRestore.Close()}

$Search_OnClick= 
{Get-ADObject -Filter "displayName -eq '$input.text'" -IncludeDeletedObjects $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 = 241
$System_Drawing_Size.Width = 289
$ADSearchandRestore.ClientSize = $System_Drawing_Size
$ADSearchandRestore.DataBindings.DefaultDataSourceUpdateMode = 0
$ADSearchandRestore.Name = "ADSearchandRestore"
$ADSearchandRestore.Text = "AD Search & Restore"


$Results.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 125
$Results.Location = $System_Drawing_Point
$Results.Name = "Results"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 97
$System_Drawing_Size.Width = 265
$Results.Size = $System_Drawing_Size
$Results.TabIndex = 5
$Results.UseCompatibleStateImageBehavior = $False
$Results.View = 3

$ADSearchandRestore.Controls.Add($Results)

$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,2,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
$Input.Text = "John Doe"

$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 form loads fine, it allows me to enter a display name and hit search. It even loads the activedirectory module but then throws this error

Get-ADObject : A positional parameter cannot be found that accepts argument 'True'.
At H:zsearch.ps1:45 char:2
+ {Get-ADObject -Filter "displayName -eq '$input.text'" -IncludeDeletedObjects $Re ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADObject


Please help! I'm going gray and tearing my hair out

Re: Get-adobject doesn't work using GUI

Posted: Fri Jan 31, 2014 3:45 pm
by jvierra
Try:
PowerShell Code
Double-click the code block to select all.
$Search_OnClick={
     Get-ADObject -Filter {displayName -eq '$($input.text)'} -IncludeDeletedObjects 
     $Results.Focus()
}

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 7:12 am
by daviesg6
Thanks, I no longer get the error, but I don't get any output either. I guess I now need to work on getting the output in the results box. While I try to work that one out, anyone have any suggestions that may help get this result?

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 7:36 am
by jvierra
You have to capture the results of the Get-AdObject and apply them to a control.

Start here: http://www.sapien.com/blog/topics/user- ... istrators/

Once you understand how Forms work and are designed you will see ways to get output from CmdLets into controls.

I believe this should be the correct version:
PowerShell Code
Double-click the code block to select all.
$Search_OnClick={
     $results=Get-ADObject -Filter "displayName -eq '$($input.text)'}" -IncludeDeletedObjects | Out-String
     $Results.Focus()
}

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 9:29 am
by daviesg6
Again, thank you. I tried the code you posted, it now gives the same error I previously had. The link you sent is useful, but it still seems I can either have an error or no results :(

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 11:48 am
by daviesg6
I've been playing with this, and now get a little closer, the issue now is it tells me
Method invocation failed because [System.String] does not contain a method named 'Focus'.
At line:52 char:6
+ $Results.Focus()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound


I tried changing it to output the results to a txt file, but it didn't find anything, or at least it didn't populate the file with anything, which is weird because I'm searching for my own account. this is found if I run the search function manually.

I'm seriously thinking of just forgetting the whole thing and trying to sew together some holes to make a fishing net :?

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 1:04 pm
by jvierra
Sorry - the code should read:
PowerShell Code
Double-click the code block to select all.
$Search_OnClick={
     $results.Text=Get-ADObject -Filter "displayName -eq '$($input.text)'}" -IncludeDeletedObjects | Out-String
     $Results.Focus()
}
Assuming $results is a text control.

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 1:31 pm
by daviesg6
Isn't there an extra } in there? (highlighted in red below)
$Search_OnClick={
$results.Text=Get-ADObject -Filter "displayName -eq '$($input.text)'}" -IncludeDeletedObjects | Out-String
$Results.Focus()
}

If I remove that I again get

Get-ADObject : The search filter cannot be recognized
At H:zzsearch.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


This does not make sense, it's saying Get-ADObject is not a valid cmdlet.

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 2:23 pm
by jvierra
I tried to delete it.

It is so hard to edit this stuff in these lame web editors.
PowerShell Code
Double-click the code block to select all.
$Search_OnClick={
     $results.Text=Get-ADObject -Filter "displayName -eq '$($input.text)'" -IncludeDeletedObjects | Out-String
     $Results.Focus()
}

Re: Get-adobject doesn't work using GUI

Posted: Mon Feb 03, 2014 2:45 pm
by daviesg6
I had already removed it from the script, the problem is even with the extra } removed, it still gives

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


which is what's confusing me. It is saying get-adobject is not a cmdlet, line 51 character 20 is the G in Get-ADObject