So I have a script that works perfectly. I am trying to turn this script into a function so I can run it directly from the console. My current script uses VB to have a popup window for user input. I am trying to modify this. This is what I have so far. The first script is the one that doesn’t work. The second one works I just want it to be a function. I want it to work by either accepting -username switch OR asking for it if you don’t use the switch. Currently it is doing both
function Unlock-User {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string[]]$username
)
PROCESS {
#$username = Read-Host -Prompt 'Input Username'
$words = @('Red','Orange','Yellow','Green','Blue','Purple','White','Black')
$word = $words | Get-Random
$number = '{0:D2}' -f (Get-Random -Maximum 99)
$password="$word$number"
Unlock-ADAccount -Identity $username
Set-ADAccountPassword -Identity $username -NewPassword (ConvertTo-SecureString -AsPlainText "$password" -Force)
Set-ADUser -Identity $username -ChangePasswordAtLogon $true -PasswordNeverExpires $false
Write-Output "User account $username has been unlocked, and password has been reset to $password"
}
END {}
}
##################################################################
###
###
###
###
###
### This code unlocks a user account and resets the password.
### It includes capitals, lowercase, and numbers. Easy to remember.
###
###
###
###
##################################################################
######################################################################
### I don't know what this does but it hides the PowerShell window ###
######################################################################
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
##############################
### Load Visual Basic Shit ###
##############################
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
Add-Type -AssemblyName PresentationCore,PresentationFramework
###########################################################
### Make buttons, icons, and textbox for easier reading ###
###########################################################
$ButtonType = [System.Windows.MessageBoxButton]::OK
$MessageIcon = [System.Windows.MessageBoxImage]::Information
$MessageTitle = "Account Unlocked!"
###########################
### Ask for a user name ###
###########################
$username = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a username", "Username")
#######################
### Word dictionary ###
#######################
$words = @('Red','Orange','Yellow','Green','Blue','Purple','White','Black')
##########################
### Pick a random word ###
##########################
$word = $words | Get-Random
#########################################
### Get a random number with 2 digits ###
#########################################
$number = '{0:D2}' -f (Get-Random -Maximum 99)
#############################
### Build password string ###
#############################
$password="$word$number"
#########################
### Unlock AD account ###
#########################
Unlock-ADAccount -Identity $username
#######################################
### Reset password and force change ###
#######################################
Set-ADAccountPassword -Identity $username -NewPassword (ConvertTo-SecureString -AsPlainText "$password" -Force)
Set-ADUser -Identity $username -ChangePasswordAtLogon $true -PasswordNeverExpires $false
########################
### Message box text ###
########################
$MessageBody = "User account $username has been unlocked, and password has been reset to $password"
############################
### Display confirmation ###
############################
[System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)