45 lines
1.7 KiB
PowerShell
45 lines
1.7 KiB
PowerShell
|
function Get-CoalescedStringValue {
|
||
|
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Simple null coalesce function for string values. Null, empty, and whitespace strings will return the second provided value
|
||
|
.DESCRIPTION
|
||
|
Simple null coalesce function for string values. Null, empty, and whitespace strings will return the second provided value
|
||
|
.PARAMETER ValueA
|
||
|
Value to test for null/empty equivalency. This value is returned it is not equivalent to null, empty, or whitespace only.
|
||
|
.PARAMETER ValueB
|
||
|
Value to return if ValueA is equivalent to null, empty, or whitespace
|
||
|
.NOTES
|
||
|
Similar in function to Test-IsNull, but written to begin moving away from that poorly named and convoluted function, focusing on string values
|
||
|
#>
|
||
|
|
||
|
[CmdletBinding()]
|
||
|
[OutputType([System.String])]
|
||
|
param (
|
||
|
[Parameter(Mandatory=$false)]
|
||
|
[Alias("TestValue")]
|
||
|
[string]$ValueA = $null,
|
||
|
|
||
|
[Parameter(Mandatory=$false)]
|
||
|
[Alias("FallbackValue")]
|
||
|
[string]$ValueB
|
||
|
)
|
||
|
|
||
|
$logLead = Get-LogLeadName
|
||
|
$valueAIsNullEmptyOrWhitespace = Test-StringIsNullOrWhitespace -Value $ValueA
|
||
|
|
||
|
if ($valueAIsNullEmptyOrWhitespace) {
|
||
|
|
||
|
$valueBIsNullEmptyOrWhitespace = Test-StringIsNullOrWhitespace -Value $ValueB
|
||
|
if ($valueBIsNullEmptyOrWhitespace) {
|
||
|
|
||
|
$nullValueBWarningMessage = "$logLead : Value B is null, empty, or whitespace. ValueB will be returned; however, assumptions in the calling code should be checked for " + `
|
||
|
"appropriate handling. Note that PowerShell will coerce a null value to an empty string."
|
||
|
Write-Warning $nullValueBWarningMessage
|
||
|
}
|
||
|
|
||
|
return $ValueB
|
||
|
}
|
||
|
|
||
|
return $ValueA
|
||
|
}
|