Variable null PowerShell

De Linuxmemo.


[modifier] test sur variable null ou vide

function IsNull($objectToCheck) {
    if ($objectToCheck -eq $null) {
        return $true
    }

    if ($objectToCheck -is [String] -and $objectToCheck -eq [String]::Empty) {
        return $true
    }

    if ($objectToCheck -is [DBNull] -or $objectToCheck -is [System.Management.Automation.Language.NullString]) {
        return $true
    }

    return $false
}

IsNull($null)
# True

IsNull($c) # Where $c wasn't declared yet
# True

IsNull([string] $c)
# True

IsNull("")
# True

IsNull([DBNull]::Value)
# True

IsNull(1)
# False

IsNull(New-Object System.DateTime)
# False

IsNull("Looking Good")
# False

IsNull(0)
# False
Outils personnels