How to customize error message

Product: PowerShell Universal
Version: 1.5

image
Set-PSUAuthenticationMethod -ScriptBlock {

param(

[PSCredential]$Credential

)

You can call whatever cmdlets you like to conduct authentication here.

Just make sure to return the $Result with the Success property set to $true

$Result = [Security.AuthenticationResult]::new()

$user_name = $Credential.GetNetworkCredential().UserName

$password = $Credential.GetNetworkCredential().Password

try

{

$is_authen = Get-ADUser -Identity $user_name -credential $Credential | Select-Object -First 1

}

catch

{

$is_authen = $null

}

if ($is_authen)

{

$Result.UserName = $user_name

$Result.Success = $true 

$Result

}

else

{

$Result.Success = $false

$Result.ErrorMessage = "error"

}

#$Result #.ErrorMessage = “error”

#New-UDAuthenticationResult -ErrorMessage “You aren’t Adam!!!”

}

fix by this
New-PSUAuthenticationResult -ErrorMessage ‘Error. Bad username or password’

1 Like