Functions/Validations/Test-StringNullOrEmpty.ps1
<#
.SYNOPSIS Checks if string variable is null or empty. In case of switch GiveErrorIfEmpty there will be an error. .DESCRIPTION Checks if string variable is null or empty. In case of switch GiveErrorIfEmpty there will be an error. .EXAMPLE if (Test-StringNullOrEmpty -VariableName 'ProjectNo') {...} .EXAMPLE Test-StringNullOrEmpty -VariableName 'ItemNo' -GiveErrorIfEmpty .PARAMETER $VariableName The name of the variable to check. .PARAMETER GiveErrorIfEmpty Switch to determine if error must be given. #> function Test-StringNullOrEmpty { [CmdletBinding()] [Alias('Check-StringNullOrEmpty')] param ( [parameter(Mandatory=$true)] [string] $VariableName, [switch] $GiveErrorIfEmpty ) PROCESS { if ([string]::IsNullorEmpty((Get-Variable -Name $VariableName -Scope script -ErrorAction SilentlyContinue).Value)) { if ($GiveErrorIfEmpty -eq $false) { return $true } else { Write-Error ("Variable '{0}' must be set." -f $VariableName) -ErrorAction Stop } } } } Export-ModuleMember -Function Test-StringNullOrEmpty -Alias Check-StringNullOrEmpty |