Powershell: How do I pass secure credentials to 'test-adauthentication'?

Kelly Sparks 121 Reputation points
2021-07-14T13:20:08.277+00:00

I have a valid set of credentials stored in $Cred.

$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password

Clearly they work like this:
PS C:\> $login=Test-ADAuthentication -username $Cred.UserName -password $Cred.GetNetworkCredential().Password
PS C:\> echo $login
True

However, I would like to pass them using only the $Cred variable if possible.
I have tried both of these, but neither work.

PS C:\> $login=Test-ADAuthentication -credential $Cred
PS C:\> echo $login
False
PS C:\> $login=Test-ADAuthentication -PSCredential $Cred
PS C:\> echo $login
False

Any help on this one?

Note: the only tag I found that was close to my question, was azure-ad-authentication-protocols... which isn't actually about my question, but I couldn;t submit the question without a tag.. so..

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,524 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kelly Sparks 121 Reputation points
    2021-07-15T11:22:52.957+00:00

    I ultimately figured this out... the Get-ADUser function has an option to pass $Cred, and if $Cred is wrong, it fails.

    try
    {
    $login=Get-ADUser -Identity $User -Credential $Cred -Server $Domain
    }
    catch
    {
    $login=$false
    Write-Host("Login failed!")
    }


2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 46,711 Reputation points
    2021-07-15T02:23:02.593+00:00

    "Test-ADAuthentication" is a function that either you or someone else wrote. Is that correct? If so, see this: add-credentials-to-powershell-functions. The "Adding a Credential Parameter" section should help.

    You can also pass the user-id as a string and the password as a secure string, and then create the credential in the function.


  2. Ian Xue 36,936 Reputation points Microsoft Vendor
    2021-07-15T05:49:33.1+00:00

    Hi

    What is Test-ADAuthentication? If it's a function, you can replace the parameters $username and $password with $credential, then add the below lines to the function.

    $username = $credential.UserName  
    $password = $credential.GetNetworkCredential().Password  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.