Define Powershell function that takes a PSCredential object, but won't prompt if it's null?
<p>I have a Powershell function that takes a PSCredential object. If the PSCredential is null, I want to handle that specially in the function myself. However, what ends up happening is I get a PSCredential prompt (the same prompt as when you run <code>Get-Credential</code>).</p>

<p><a href="https://i.stack.imgur.com/QL8OI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QL8OI.png" alt="PSCredential prompt example"></a></p>

<p>In my script below, I make the parameter required for the <code>Credential</code> parameter set, and I wanted to make it nullable with <code>[AllowNull()]</code>, but that didn't stop the prompt from appearing.</p>

<p>Here's my function: </p>

<pre><code>&lt;#
.description
Create a new Connection String Builder object
.parameter existingCSB
Use the passed CSB as the base, but allow it to be overridden.
.parameter property
Hashtable containing named properties for a connection string builder.
Will override values on -existingCSB.
.parameter credential
Use these credentials. Will override values on -existingCSB and any values
passed as -property.
NOTE: If this parameter is null, or if EITHER the username OR password in
the credential is an empty string, use a trusted connection aka 
integrated security instead
.parameter username
Use this username. Will override values on -existingCSB and any values
passed as -property
NOTE: If EITHER the username OR password is null or an empty string, use a
trusted connection / integrated security instead
.parameter password
Use this password. Can be a string or a SecureString object. Will override
values on -existingCSB and any values passed as -property
NOTE: If EITHER the username OR password is null or an empty string, use a
trusted connection / integrated security instead
#&gt;
function New-DbConnectionStringBuilder {
    [cmdletbinding(DefaultParameterSetName="NoCredential")] param(
        [System.Data.Common.DbConnectionStringBuilder] $existingCSB,

        [Hashtable] $property,

        [Parameter(Mandatory=$true, ParameterSetName="Credential")]
        [AllowNull()] [System.Management.Automation.PSCredential] $credential,

        [Parameter(Mandatory=$true, ParameterSetName="UserPass")]
        [AllowEmptyString()] [string] $username,

        [Parameter(Mandatory=$true, ParameterSetName="UserPass")]
        [AllowNull()] $password
    )
    $newDbCSB = New-Object System.Data.Common.DbConnectionStringBuilder $true
    if ($existingCSB.Keys.Count -gt 0) {
        $existingCSB.Keys |% { $newDbCSB[$_] = [String]::Copy($existingCSB[$_]) }
    }
    if ($property.Keys.Count -gt 0) {
        $property.Keys |% { $newDbCSB[$_] = [String]::Copy($property[$_]) }
    }

    if ($PsCmdlet.ParameterSetName -notmatch "NoCredential") {
        if ($credential) {
            $username = $credential.UserName
            $password = $credential.Password
        }
        if ($password -and $password.GetType().FullName -eq "System.Security.SecureString") {
            $password = Decrypt-SecureString $password
        }

        if ($username -and $password) {
            $newDbCSB["Uid"] = $username
            $newDbCSB["Pwd"] = $password
            $newDbCSB.Remove("Trusted_Connection") | Out-Null
        }
        else {
            $newDbCSB["Uid"] = $null
            $newDbCSB["Pwd"] = $null
            $newDbCSB["Trusted_Connection"] = "yes"
        }
    }

    return $newDbCSB
}
</code></pre>

<p>Here's some code to call it:</p>

<pre><code>$csb = New-DbConnectionStringBuilder -credential $null
Write-Host "UID: $($csb['Uid'])"
Write-Host "Trusted_Connection: $($csb['Trusted_Connection'])"
</code></pre>

<p>In case you're interested, the reason I'm doing this is for compatibility with existing code. We have some scripts that obtain credentials from encrypted files checked into Git. If a credential isn't present, the existing code will return $null as an indication that integrated security should be used instead.</p>

<p>The behavior I want, summarized, is: if no credentials are passed at all, do not add credential information to the CSB; if credentials are passed but $null, then use integrated security (aka a "trusted connection"); if credentials are passed and not null, then use them in the CSB.</p>