PowerShell Guru - Request
I'm curious if any of the PowerShell Guru's would help me with a request.
I have a script I use once in awhile that I'd like to be setup in a GUI style.
Script just "extends" the users password. Basically just resets the Last Reset Date. Currently I have to "edit" and then enter the username, save it then run the script. I'd love it if I could just have a Username Box and a Run Button so i can just open it up type the username, and hit run then it executes the script. Any takers for help ?? :)
Import-Module ActiveDirectory
$users = "gerdesa" # enter USERNAME
foreach ($user in $users)
{
Get-ADUser $user | Set-ADAccountControl -PasswordNeverExpires $false
$TargetUser = Get-ADUser -Filter {sAMAccountName -eq $user}
$uObj = [ADSI]"LDAP://$TargetUser"
$uObj.put("pwdLastSet", 0)
$uObj.SetInfo()
$uObj.put("pwdLastSet", -1)
$uObj.SetInfo()
}
-
Actually Just do this:
[array]$users = (Read-Host "Enter Usernames separated by a comma:").Split(",") | % { $_.Trim() }
When you run the script it'll prompt for the users you want to change, comma separated-like. this becomes either a single or multiple member array depending on how many users you enter. The rest of your code will work as advertised.
-
Basically this: Just add the code at the top?
[array]$users = (Read-Host "Enter Usernames separated by a comma:").Split(",") | % { $_.Trim() }
Import-Module ActiveDirectory
$users = "gerdesa" # enter USERNAME
foreach ($user in $users)
{
Get-ADUser $user | Set-ADAccountControl -PasswordNeverExpires $false
$TargetUser = Get-ADUser -Filter {sAMAccountName -eq $user}
$uObj = [ADSI]"LDAP://$TargetUser"
$uObj.put("pwdLastSet", 0)
$uObj.SetInfo()
$uObj.put("pwdLastSet", -1)
$uObj.SetInfo()
} -
If you use "param ()" you can take command line parameters.
Import-Module ActiveDirectory
param (
[string]$users = ($throw "You must specify a list of users")
)
foreach ($user in $users)
{
Get-ADUser $user | Set-ADAccountControl -PasswordNeverExpires $false
$TargetUser = Get-ADUser -Filter {SAMAccountName -eq $user}
$uObj = [ADSI]"LDAP://$TargetUser"
$uObj.put("pwdLastSet", 0)
$uObj.SetInfo()
$uObj.put("pwdLastSet", -1)
$uObj.SetInfo()
}Now you should be able to run the script like this:
.\script.ps1 "bob"
.\script.ps1 "bob","jane","bill" -
Import-Module ActiveDirectory
[array]$users = (Read-Host "Enter Usernames separated by a comma:").Split(",") | % { $_.Trim() }
foreach ($user in $users)
{
Get-ADUser $user | Set-ADAccountControl -PasswordNeverExpires $false
$TargetUser = Get-ADUser -Filter {sAMAccountName -eq $user}
$uObj = [ADSI]"LDAP://$TargetUser"
$uObj.put("pwdLastSet", 0)
$uObj.SetInfo()
$uObj.put("pwdLastSet", -1)
$uObj.SetInfo()
} -
Oops, $users should probably be an array.
Import-Module ActiveDirectory
param (
[array]$users = ($throw "You must specify a list of users")
)
foreach ($user in $users)
{
Get-ADUser $user | Set-ADAccountControl -PasswordNeverExpires $false
$TargetUser = Get-ADUser -Filter {SAMAccountName -eq $user}
$uObj = [ADSI]"LDAP://$TargetUser"
$uObj.put("pwdLastSet", 0)
$uObj.SetInfo()
$uObj.put("pwdLastSet", -1)
$uObj.SetInfo()
} -
Gah, I really should test things before posting them. It's $(throw, not ($throw
Import-Module ActiveDirectory
param (
[array]$users = $(throw "You must specify a list of users")
)
foreach ($user in $users)
{
Get-ADUser $user | Set-ADAccountControl -PasswordNeverExpires $false
$TargetUser = Get-ADUser -Filter {SAMAccountName -eq $user}
$uObj = [ADSI]"LDAP://$TargetUser"
$uObj.put("pwdLastSet", 0)
$uObj.SetInfo()
$uObj.put("pwdLastSet", -1)
$uObj.SetInfo()
}Also, you don't have to wrap the parameters in quotes.
.\script.ps1 bob
.\script.ps1 bob,jane,bill -
https://github.com/steviecoaster/Powershell/blob/master/Sadler.zip
Should be able to download that.The executable should be all you need to worry about, but I gave you the source code too incase you ever decide to get Powershell Studio :)
Double click and run PS Password Expiration Utility.exe and test it out. Let me know if you get any funky errors. I tested it with some Write-Hosts, but didn't test the actual code against my AD, for reasons haha.
-
On A Server 2008 R2 machine i get the following Error. But I was able to load it on our 2012 Server it worked just fine so far!
Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: PS Password Expiration Utility
Problem Signature 02: 1.0.0.7
Problem Signature 03: 57a9c0dd
Problem Signature 04: PoshExeHostWinV3
Problem Signature 05: 1.0.0.7
Problem Signature 06: 57a9c0dd
Problem Signature 07: 94
Problem Signature 08: 89
Problem Signature 09: System.IO.FileNotFoundException
OS Version: 6.1.7601.2.1.0.272.7
Locale ID: 1033
Additional Information 1: e6e6
Additional Information 2: e6e6c208b90db2aa028f8f653cabbf23
Additional Information 3: f3f8
Additional Information 4: f3f81af0c125b5f6031976d9c59b8d7a
-
What version of .Net Framework is on the 2008 R2 box? It's definitely something with the .Net versions.
If you have Remote Server Admin Tools on your local machine, you can use the utility from there too. the AD Powershell module gets installed as part of that, barring of course that you have elected to install the AD DS tools as part of the RSAT installation :).
Please sign in to leave a comment.
Comments
39 comments