Skip to content

Instantly share code, notes, and snippets.

@jkavanagh58
Created March 6, 2017 18:57
Show Gist options
  • Save jkavanagh58/58cb6cf2227eb473925944a030c94748 to your computer and use it in GitHub Desktop.
Save jkavanagh58/58cb6cf2227eb473925944a030c94748 to your computer and use it in GitHub Desktop.
# Qualified Function for unlocking an AD account
function unlock-anaccount {
<#
.SYNOPSIS
Used to unlock an AD account
.DESCRIPTION
Using the provided logon name this function will attempt to unlock the AD account.
.PARAMETER logonName
This value should match the User Account samAccountName aka User Logon Name from AD Users and Computers.
.EXAMPLE
PS C:\> unlock-anaccount -logonName 'Value1'
.NOTES
03.06.2017 JJK: Function written and added to profile.ps1
03.06.2017 JJK: TODO: Modify paramater validation to perform search-adaccount to consolidate validating
account and validating it is currently locked out
TODO: Validate user account attempting to unlock the account has permissions
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
Position = 1,
HelpMessage = 'Enter the samAccountName for the account to be unlocked.')]
[ValidateScript({ get-aduser $_ })]
[System.String]$logonName
)
Begin {
"Account validated, attempting to unlock the account"
}
Process {
If ((Get-ADUser -Identity $logonName -Properties LockedOut).LockedOut) {
# Get the User Account from AD
$acctDetails = Get-ADUser -Identity $logonName
"Unlocking Account for {0}" -f $acctDetails.Name
try {
$acctDetails | Unlock-ADAccount -Verbose -ErrorAction SilentlyContinue
}
Catch {
"Unable to unlock account"
$Error[0].ErrorDetails.Message
}
}
Else {
"The account for {0} is not currently LockedOut" -f $acctDetails.Name
}
}
End {
"Finished processing Unlock task for {0}" -f $logonName
}
} # Unlock-anAccount function end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment