<commit_before><#
    .SYNOPSIS
    Test the provided credentials with the choosen test method.

    .DESCRIPTION
    Test the provided credentials against the local system by starting a simple
    process or against Active Directory by binding to the root via ADSI.

    .INPUTS
    None.

    .OUTPUTS
    System.Boolean. Indicates if the credentials are valid or not.

    .EXAMPLE
    PS C:\> Test-Credential -Credential 'DOMAIN\user'
    Test the interactive provided credentials against the local system.

    .EXAMPLE
    PS C:\> Test-Credential -Username $Username -Password $Password -Method ActiveDirectory
    Test the provided username and password pair against the Active Directory.

    .NOTES
    Author     : <NAME>
    License    : MIT License

    .LINK
    https://github.com/claudiospizzi/SecurityFever
#>

function Test-Credential
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        # PowerShell credentials object to test.
        [Parameter(Mandatory = $true, ParameterSetName = 'Credential')]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential,

        # The username to validate. Specify password too.
        [Parameter(Mandatory = $true, ParameterSetName = 'UsernamePassword')]
        [System.String]
        $Username,

        # The password to validate. Specify username too.
        [Parameter(Mandatory = $true, ParameterSetName = 'UsernamePassword')]
        [System.Security.SecureString]
        $Password,

        # Validation method.
        [Parameter(Mandatory = $false)]
        [ValidateSet('StartProcess', 'ActiveDirectory')]
        [System.String]
        $Method = 'StartProcess'
    )

    begin
    {
        if ($PSCmdlet.ParameterSetName -eq 'UsernamePassword')
        {
            $Credential = New-Object -TypeName PSCredential -ArgumentList $Username, $Password
        }
    }

    process
    {
        try
        {
            if ($Method -eq 'StartProcess')
            {
                # Create a new local process with the given credentials. This
                # does not validate the credentials against a target system, but
                # tests if they are valid locally.
                $startInfo = New-Object -TypeName System.Diagnostics.ProcessStartInfo
                $startInfo.FileName        = 'cmd.exe'
                $startInfo.Arguments       = '/C', 'echo %USERDOMAIN%\%USERNAME%'
                $startInfo.Domain          = $Credential.GetNetworkCredential().Domain
                $startInfo.UserName        = $Credential.GetNetworkCredential().UserName
                $startInfo.Password        = $Credential.GetNetworkCredential().SecurePassword
                $startInfo.WindowStyle     = [System.Diagnostics.ProcessWindowStyle]::Hidden
                $startInfo.CreateNoWindow  = $true
                $startInfo.UseShellExecute = $false

                $process = New-Object -TypeName  System.Diagnostics.Process
                $process.StartInfo = $startInfo
                $process.Start() | Out-Null
            }

            if ($Method -eq 'ActiveDirectory')
            {
                # We use an empty path, because we just test the credential
                # binding and not any object access in Active Directory.
                $directoryEntryArgs = @{
                     TypeName     = 'System.DirectoryServices.DirectoryEntry'
                     ArgumentList = '',
                                    $Credential.GetNetworkCredential().UserName,
                                    $Credential.GetNetworkCredential().Password
                }
                $directoryEntry = New-Object @directoryEntryArgs

                if ($null -eq $directoryEntry)
                {
                    throw 'Unable to create an ADSI connection.'
                }
            }

            return $true
        }
        catch
        {
            Write-Warning -Message "Credential validation failed: $_"

            return $false
        }
    }
}<commit_msg>Add throw parameter to Test-Credential<commit_after><#
    .SYNOPSIS
    Test the provided credentials with the choosen test method.

    .DESCRIPTION
    Test the provided credentials against the local system by starting a simple
    process or against Active Directory by binding to the root via ADSI.

    .INPUTS
    None.

    .OUTPUTS
    System.Boolean. Indicates if the credentials are valid or not.

    .EXAMPLE
    PS C:\> Test-Credential -Credential 'DOMAIN\user'
    Test the interactive provided credentials against the local system.

    .EXAMPLE
    PS C:\> Test-Credential -Credential 'DOMAIN\user' -Throw
    Test the interactive provided credentials against the local system, and
    throws an exception if the credentials are not valid.

    .EXAMPLE
    PS C:\> Test-Credential -Username $Username -Password $Password -Method ActiveDirectory
    Test the provided username and password pair against the Active Directory.

    .NOTES
    Author     : <NAME>
    License    : MIT License

    .LINK
    https://github.com/claudiospizzi/SecurityFever
#>

function Test-Credential
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        # PowerShell credentials object to test.
        [Parameter(Mandatory = $true, ParameterSetName = 'Credential')]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential,

        # The username to validate. Specify password too.
        [Parameter(Mandatory = $true, ParameterSetName = 'UsernamePassword')]
        [System.String]
        $Username,

        # The password to validate. Specify username too.
        [Parameter(Mandatory = $true, ParameterSetName = 'UsernamePassword')]
        [System.Security.SecureString]
        $Password,

        # Validation method.
        [Parameter(Mandatory = $false)]
        [ValidateSet('StartProcess', 'ActiveDirectory')]
        [System.String]
        $Method = 'StartProcess',

        # Throw an terminating exception if the credentials are not valid.
        [Parameter(Mandatory = $false)]
        [System.Management.Automation.SwitchParameter]
        $Throw
    )

    begin
    {
        if ($PSCmdlet.ParameterSetName -eq 'UsernamePassword')
        {
            $Credential = New-Object -TypeName PSCredential -ArgumentList $Username, $Password
        }
    }

    process
    {
        try
        {
            if ($Method -eq 'StartProcess')
            {
                # Create a new local process with the given credentials. This
                # does not validate the credentials against an external target
                # system, but tests if they are valid locally. Of courese, it's
                # possible to validate domain credentials too.
                $startInfo = New-Object -TypeName System.Diagnostics.ProcessStartInfo
                $startInfo.FileName        = 'cmd.exe'
                $startInfo.Arguments       = '/C', 'echo %USERDOMAIN%\%USERNAME%'
                $startInfo.Domain          = $Credential.GetNetworkCredential().Domain
                $startInfo.UserName        = $Credential.GetNetworkCredential().UserName
                $startInfo.Password        = $Credential.GetNetworkCredential().SecurePassword
                $startInfo.WindowStyle     = [System.Diagnostics.ProcessWindowStyle]::Hidden
                $startInfo.CreateNoWindow  = $true
                $startInfo.UseShellExecute = $false

                $process = New-Object -TypeName  System.Diagnostics.Process
                $process.StartInfo = $startInfo
                $process.Start() | Out-Null
            }

            if ($Method -eq 'ActiveDirectory')
            {
                # We use an empty path, because we just test the credential
                # binding and not any object access in Active Directory.
                $directoryEntryArgs = @{
                     TypeName     = 'System.DirectoryServices.DirectoryEntry'
                     ArgumentList = '',
                                    $Credential.GetNetworkCredential().UserName,
                                    $Credential.GetNetworkCredential().Password
                }
                $directoryEntry = New-Object @directoryEntryArgs

                if ($null -eq $directoryEntry)
                {
                    throw 'Unable to create an ADSI connection.'
                }
            }

            if (-not $Throw.IsPresent)
            {
                return $true
            }
        }
        catch
        {
            Write-Warning -Message "Credential validation failed: $_"

            if ($Throw.IsPresent)
            {
                throw "Credential validation failed: $_"
            }
            else
            {
                return $false
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08062345817447858, 'fraction_numerical': 0.0, 'mean_word_length': 1.772458812558284, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5520941', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2131, 'n_words': 608}