Functions/Check-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 (Check-StringNullOrEmpty -VariableName 'ProjectNo') {...}
    .EXAMPLE
    Check-StringNullOrEmpty -VariableName 'ItemNo' -GiveErrorIfEmpty
    .PARAMETER $VariableName
    The name of the variable to check.
    .PARAMETER GiveErrorIfEmpty
    Switch to determine if error must be given.
#>


function Check-StringNullOrEmpty
{
    param
    (
        [parameter(Mandatory=$true)]
        [string] $VariableName,
        [switch] $GiveErrorIfEmpty
    )
    PROCESS
    {
        if ([string]::IsNullorEmpty((Get-Variable -Name $VariableName -ErrorAction SilentlyContinue).Value)) {
            if ($GiveErrorIfEmpty -eq $false) {
                return $true
            } else {
                Write-Error ("Variable '{0}' must be set." -f $VariableName) -ErrorAction Stop
            }
        }
    }
}

Export-ModuleMember -Function Check-StringNullOrEmpty