Setting AD Password from GUI

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 7 years 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
nikedia
Posts: 5
Last visit: Thu Aug 17, 2023 7:08 am

Setting AD Password from GUI

Post by nikedia »

Hi All,

I am trying to create this application to amend a users password dependent on the value within a label. I have created a text box to hold the value which is generated from our system. Preferably I would like it to have one special character and 1 number to keep it secure but I am not sure how to make this simple, I have also hit a road block when it attempts to change the password, AD Powershell states that it is not able to make the text value a secure string. The code is:

Powershell GUI Script:

Add-Type -AssemblyName System.Web
$Assembly = [System.Web.Security.Membership]::GeneratePassword(8, 1)
$richtextbox1.text = $Assembly
$Generated.text = $Assembly
$password = $richtextbox1.Text | ConvertTo-SecureString -AsPlainText -Force
$users = $username.Text | ConvertTo-SecureString -AsPlainText -Force


Set-ADAccountPassword $username.Text -NewPassword $richtextbox1.text -verbose -Reset -PassThru | Set-ADuser $users -ChangePasswordAtNextLogon $true
"Exported Log: $user, $Generated" | out-file output\Output.log -append


Powershell Output:

Set-ADAccountPassword : Cannot bind parameter 'NewPassword'. Cannot convert the "tBG.uvAV" value of type "System.String" to type "System.Security.SecureString".
At Export.ps1:17899 char:52
+ Set-ADAccountPassword $username.Text -NewPassword <<<< $richtextbox1.text -verbose -Reset -PassThru | Set-ADuser $users -ChangePasswordAtNextLogon $true
+ CategoryInfo : InvalidArgument: (:) [Set-ADAccountPassword], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.SetADAccountPassword


Many Thanks all, Craig
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Setting AD Password from GUI

Post by jvierra »

We do not encrypt the username.

Your code does not make much sense. Why are you using "$richtextbox1" when you want a secure string. a Textbox cannot hold a secure string. It can only contain text.
User avatar
nikedia
Posts: 5
Last visit: Thu Aug 17, 2023 7:08 am

Re: Setting AD Password from GUI

Post by nikedia »

I attempted to just use a label but it didn't work so I tried a text box instead but it still failed
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Setting AD Password from GUI

Post by jvierra »

  1. Add-Type -AssemblyName System.Web
  2. $richtextbox1.text = [System.Web.Security.Membership]::GeneratePassword(8, 1)
  3. $password = $richtextbox1.Text | ConvertTo-SecureString -AsPlainText -Force
  4. Set-ADAccountPassword $username.Text -NewPassword $password -Reset
  5. Set-ADuser $username.Text -ChangePasswordAtLogon $true
  6. $msg = 'Exported Log:{}{}' -f $username.Text, $richtextbox1.text
  7. Write-Host $msg
  8. $msg | out-file output\Output.log -append
This topic is 7 years 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