Public/Find-LockedAccount.ps1

Function Find-LockedAccount {
    <#
        .Synopsis
            Find locked ActiveDirectory accounts
        .Description
            The Find-LockedAccount function retrieves one or more user accounts that meet the criteria specified by the parameters. The Search can be limited to a specific base or scope by setting the SearchBase or SearchScope parameters.
        .Parameter AuthType
            Specifies the authentication method to use. Possible values for this parameter include:
 
            Negotiate or 0
 
            Basic or 1
 
            The default authentication method is Negotiate.
 
            A Secure Sockets Layer (SSL) connection is required for the Basic authentication method.
 
            The following example shows how to set this parameter to Basic.
 
            -AuthType Basic
 
        .Parameter Credentials
            Specifies the user account credentials to use to perform this task. The default credentials are the credentials of the currently logged on user unless the cmdlet is run from an Active Directory PowerShell provider drive. If the cmdlet is run from such a
            provider drive, the account associated with the drive is the default.
 
            To specify this parameter, you can type a user name, such as "User1" or "Domain01\User01" or you can specify a PSCredential object. If you specify a user name for this parameter, the cmdlet prompts for a password.
 
            You can also create a PSCredential object by using a script or by using the Get-Credential cmdlet. You can then set the Credential parameter to the PSCredential object The following example shows how to create credentials.
 
            $AdminCredentials = Get-Credential "Domain01\User01"
 
            The following shows how to set the Credential parameter to these credentials.
 
            -Credential $AdminCredentials
 
            If the acting credentials do not have directory-level permission to perform the task, Active Directory PowerShell returns a terminating error.
 
        .Parameter ResultPageSize
            Specifies the number of objects to include in one page for an Active Directory Domain Services query.
 
            The default is 256 objects per page.
 
            The following example shows how to set this parameter.
 
            -ResultPageSize 500
 
        .Parameter ResultSetSize
            Specifies the maximum number of objects to return for an Active Directory Domain Services query. If you want to receive all of the objects, set
            this parameter to $null (null value). You can use Ctrl+c to stop the query and return of objects.
 
            The default is $null.
 
            The following example shows how to set this parameter so that you receive all of the returned objects.
 
            -ResultSetSize $null
 
        .Parameter SearchBase
            Specifies an Active Directory path to search under.
 
            When you run a cmdlet from an Active Directory provider drive, the default value of this parameter is the current path of the drive.
 
            When you run a cmdlet outside of an Active Directory provider drive against an AD DS target, the default value of this parameter is the default naming context of the target domain.
 
            When you run a cmdlet outside of an Active Directory provider drive against an AD LDS target, the default value is the default naming context of the target LDS instance if one has been specified by setting the msDS-defaultNamingContext property of the Active
            Directory directory service agent (DSA) object (nTDSDSA) for the AD LDS instance. If no default naming context has been specified for the target AD LDS instance, then this parameter has no default value.
 
            The following example shows how to set this parameter to search under an OU.
 
            -SearchBase "ou=mfg,dc=noam,dc=corp,dc=contoso,dc=com"
 
            When the value of the SearchBase parameter is set to an empty string and you are connected to a GC port, all partitions will be searched. If the value of the SearchBase parameter is set to an empty string and you are not connected to a GC port, an error will be
            thrown.
 
            The following example shows how to set this parameter to an empty string. -SearchBase ""
 
        .Parameter SearchScope
            Specifies the scope of an Active Directory search. Possible values for this parameter are:
 
            Base or 0
 
            OneLevel or 1
 
            Subtree or 2
 
            A Base query searches only the current path or object. A OneLevel query searches the immediate children of that path or object. A Subtree query searches the current path or object and all children of that path or object.
 
            The following example shows how to set this parameter to a subtree search.
 
            -SearchScope Subtree
 
        .Parameter Server
            Specifies the Active Directory Domain Services instance to connect to, by providing one of the following values for a corresponding domain name or directory server. The service may be any of the following: Active Directory Lightweight Domain Services, Active
            Directory Domain Services or Active Directory Snapshot instance.
 
            Domain name values:
 
            Fully qualified domain name
 
            Examples: corp.contoso.com
 
            NetBIOS name
 
            Example: CORP
 
            Directory server values:
 
            Fully qualified directory server name
 
            Example: corp-DC12.corp.contoso.com
 
            NetBIOS name
 
            Example: corp-DC12
 
            Fully qualified directory server name and port
 
            Example: corp-DC12.corp.contoso.com:3268
 
            The default value for the Server parameter is determined by one of the following methods in the order that they are listed:
 
            -By using Server value from objects passed through the pipeline.
 
            -By using the server information associated with the Active Directory PowerShell provider drive, when running under that drive.
 
            -By using the domain of the computer running Powershell.
 
            The following example shows how to specify a full qualified domain name as the parameter value.
 
            -Server "corp.contoso.com"
 
        .Example
            PS C:\> Find-LockedAccount
 
            Lists locked ActiveDirectory accounts
 
        .Example
            PS C:\> Find-LockedAccount | Export-CSV lockedaccounts.csv
 
            Exports list of locked accounts to CSV
 
        .Inputs
            None
        .Outputs
            [System.Object]
        .LINK
            about_functions_advanced
        .LINK
            about_CommonParameters
    #>

    [CmdletBinding(
        SupportsPaging = $true,
        PositionalBinding = $false
    )]
    [OutputType('JBOADS.LockedAccount.Information')]
    Param (
        [PSCredential][System.Management.Automation.Credential()]$Credential,
        [string]$SearchBase = "",
        [string]$Server
    )
    Begin {
    }
    Process {
        Try {
            If ($PSBoundParameters['Credential']) { ($SearchADUsers).Credential = $Credential }
            If ($PSBoundParameters['SearchBase']) { ($SearchADUsers).SearchBase = $SearchBase }
            If ($PSBoundParameters['Server']) { ($SearchADUsers).Server = $Server }

            $SearchADUsers = @{
                "LockedOut"   = $true
                "UsersOnly"   = $true
                "ErrorAction" = "Stop"
            }
            $Users = Search-ADAccount @SearchADUsers

        } Catch [System.Security.Authentication.AuthenticationException] {
            Write-Warning "$($MyInvocation.MyCommand.Name):: Cannot connect to Active Directory. Invalid credentials"
        } Catch [System.Management.Automation.CommandNotFoundException] {
            Write-Warning $_.Exception.Message
            Throw $_
        } Catch [Microsoft.ActiveDirectory.Management.ADServerDownException] {
            Write-Warning "$($MyInvocation.MyCommand.Name):: Cannot connect to Active Directory, are the services running?"
        } Catch {
            Get-ErrorInfo $_
        } Finally {
            $LockedAccount = [PSCustomObject]@{
                Name           = $Users.Name
                SamAccountName = $Users.SamAccountName
                LastLogonDate  = $Users.LastLogonDate
            }
        }
    }
    End {
        Return $LockedAccount
    }
}
Set-Alias -Name Get-LockedAccount -Value Find-LockedAccount -Description "Find Locked ActiveDirectory Accounts" -Option ReadOnly -PassThru -ErrorAction SilentlyContinue