Get-adobject doesn't work using GUI

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
jvierra
Posts: 13882
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get-adobject doesn't work using GUI

Post by jvierra » Mon Feb 03, 2014 4:36 pm

Works for me.

What is $input?

$input is a special variable. Perhaps you shouldn't use it.

jvierra
Posts: 13882
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get-adobject doesn't work using GUI

Post by jvierra » Mon Feb 03, 2014 4:38 pm

Here it is working for me:
PowerShell Code
Double-click the code block to select all.
PS C:scripts> Get-ADObject -Filter "DisplayName -eq '$($test.Name)'"

DistinguishedName             Name                          ObjectClass                   ObjectGUID
-----------------             ----                          -----------                   ----------
CN=Test User01,OU=Admins,... Test User01                  user                          c2977457-41c6-4ba7-9d14-3e.

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Get-adobject doesn't work using GUI

Post by daviesg6 » Tue Feb 04, 2014 7:43 am

jvierra wrote:Works for me.

What is $input?

$input is a special variable. Perhaps you shouldn't use it.
$input is the text box where the user enters the name they are searching for. without this the entire form is useless

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Get-adobject doesn't work using GUI

Post by daviesg6 » Tue Feb 04, 2014 7:46 am

The script, without the variables created by primalforms to identify the various form elements, works fine for me. The thing is this ONLY breaks when I use the powershell code created by primalforms. that's why I'm getting frustrated with the code, it works if I don't use the form but I need to create the form so I can give this to someone else to use without having them enter code or giving them direct access to PS

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Get-adobject doesn't work using GUI

Post by daviesg6 » Tue Feb 04, 2014 8:07 am

Here is what I previously had, this creates a menu in the Powershell window. It works fine, but I have been asked to make it a form rather than "a command line operation"
This works perfectly.
PowerShell Code
Double-click the code block to select all.
Import-Module ActiveDirectory
$prompt = @"
************************************************************************************************************************
**                                      Please select from the options below                                          **
**                                                                                                                    **
**                                      s = Search AD for deleted users                                               **
**                                      r = Restore user account                                                      **
**                                      x = exit                                                                      **
**                                                                                                                    **
************************************************************************************************************************


"@
Clear-host
Do{
	$originalcolor = $host.UI.RawUI.ForegroundColor
	$host.UI.RawUI.ForegroundColor = "Yellow"
	$choice = Read-Host -Prompt $prompt
	$host.UI.RawUI.ForegroundColor = $originalcolor
	Switch($choice){
        s {$user = Read-Host 'Enter the display name to search for' ; Get-ADObject -Filter "displayName -eq '$user'" -IncludeDeletedObjects}
        r {$user = Read-Host 'Enter the display name to restore' ; Get-ADObject -Filter "displayName -eq '$user'" -IncludeDeletedObjects | Restore-ADObject}
        x {break}

		default {write-host "Invalid selection, please try again." -ForegroundColor Red}
	}
}Until($choice -eq "x")
I have had to modify the code from this to use the form, that is where it breaks. The code primalforms created does not like either Get-ADObject or the -Filter, it lists both as things it does not understand.
Using the original menu version works, using the primalforms version does not.

jvierra
Posts: 13882
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get-adobject doesn't work using GUI

Post by jvierra » Tue Feb 04, 2014 10:00 am

$input is a reserved variable. It cannot be used as a form control name. Use a different name.

Outside of a form or outside of a packaged file $input might work but should not be used.

This could be the source of your problem.

I will try and duplicate you form issue later when I get done with some projects I am in the middle of.

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Get-adobject doesn't work using GUI

Post by daviesg6 » Tue Feb 04, 2014 11:06 am

Thank you!

I changed $input to $object, and it worked! thank you very much for pointing me in the direction of this, I wasn't aware it was a reserved variable.

jvierra
Posts: 13882
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get-adobject doesn't work using GUI

Post by jvierra » Tue Feb 04, 2014 11:38 am

YOu also should not use $object. Try not to use words that are likely to be reserved or that are too general.

Controls should be named by their function. In this case the control should be named something like $DisplayName which tells us its use in the larger scheme of things.

Locked