ADAccountPassword will not work on Win10 or 2012R2 but will on 2008R2.

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
chrissmallwood
Posts: 6
Last visit: Fri Jan 05, 2024 7:34 am

ADAccountPassword will not work on Win10 or 2012R2 but will on 2008R2.

Post by chrissmallwood »

I am building a HelpDesk app to change passwords, unlock accounts, etc.

On my Win10 or 2012 R2 systems (both with RSAT installed) I can manually run the below commands (substituting the variables with non GUI versions) but am unable to do so via the GUI.

When ran through the GUI I get the error that I posted below the commands. Is this a bug with PSS?

PowerShell version 5.1 and setting the build output to Windows Form 5.

--------------------------------------

$Identity = "$($outputSelected.Text)"
$Password = "$($inputPassword.Text)"
$SecPaswd = ConvertTo-SecureString -String $Password -AsPlainText -Force
$ADUser = Set-ADAccountPassword -Reset -Identity $Identity -NewPassword $SecPaswd -PassThru -Confirm:$false -WhatIf:$false -ErrorAction Stop

------------------------------------

new : The term 'new' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At \\iqfile\it\TECH\Apps\Development\HelpDesk\HelpDesk.Run.ps1:320 char:18
+ $output.Font = new Font("Serif", 48)
+ ~~~
+ CategoryInfo : ObjectNotFound: (new:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
new : The term 'new' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At \\iqfile\it\TECH\Apps\Development\HelpDesk\HelpDesk.Run.ps1:320 char:18
+ $output.Font = new Font("Serif", 48)
+ ~~~
+ CategoryInfo : ObjectNotFound: (new:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: ADAccountPassword will not work on Win10 or 2012R2 but will on 2008R2.

Post by davidc »

It looks like you are trying to use C# code in PowerShell. Try using New-Object instead of new.
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ADAccountPassword will not work on Win10 or 2012R2 but will on 2008R2.

Post by jvierra »

The correct syntax is:

$output.Font = [System.Drawing.Font]::New('Serif', 48)

or

$output.Font = New-Object System.Drawing.Font('Serif', 48)

or
$output.Font = 'Microsoft Sans Serif, 48pt'
User avatar
chrissmallwood
Posts: 6
Last visit: Fri Jan 05, 2024 7:34 am

Re: ADAccountPassword will not work on Win10 or 2012R2 but will on 2008R2.

Post by chrissmallwood »

I'm assuming that the error is in reference to the -NewPassword flag on the password change script as that is the only place that I am using "new." That being said, does that still line up with the suggested fix?

EDIT: OK, so I see what you are referring to with the font change. I'll go ahead and update that in the script. Can you give me some suggestions as to why the script won't change the password like it does if I run it manually in ISE?
User avatar
chrissmallwood
Posts: 6
Last visit: Fri Jan 05, 2024 7:34 am

Re: ADAccountPassword will not work on Win10 or 2012R2 but will on 2008R2.

Post by chrissmallwood »

OK, not sure why but after I fixed the font issue earlier in the script it now changes the password as intended. I don't get it at all but I'm going to roll with it.

Thanks guys for your help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ADAccountPassword will not work on Win10 or 2012R2 but will on 2008R2.

Post by jvierra »

No. The error is very clear. Read the full error message. It clearly shows the line causing the error

The term 'new' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At \\iqfile\it\TECH\Apps\Development\HelpDesk\HelpDesk.Run.ps1:320 char:18
+ $output.Font = new Font("Serif", 48)

Line 320 is the cause.
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked