PowerShell 中的空變數

  1. PowerShell 中的 null 變量介紹
  2. PowerShell 中的 null 變量影響
  3. 在 PowerShell 中檢查 null 值
  4. 結論
PowerShell 中的空變數

PowerShell 將值為 null 的 $Null 對象視為 null,某些命令需要一些輸出以生成。如果有任何錯誤,它們會生成 null 值,並且在腳本中檢查命令是否生成任何值時也可以幫助故障排除。

本文談論 null 變量、在 PowerShell 中使用 null 變量的不同語法的影響,以及檢查 null 值的方法。

PowerShell 中的 null 變量介紹

我們可以將 NULL 視為未知或空的值。例如,一個變量在您分配值或對象之前是 NULL。

這個變量可能很重要,因為某些命令需要一個值,如果該值為 NULL,則會失敗。

$null 變量是 PowerShell 中的一個自動變量,用於表示 NULL。我們可以將其分配給變量,將其用於比較,並在集合中用作 NULL 的佔位符。

PowerShell 將 $null 視為一個具有 NULL 值的對象。因此,該變量與您來自其他語言的預期可能會有所不同。

PowerShell 中的 null 變量影響

$null 值的行為可能會根據它們在代碼中的出現位置而有所不同。讓我們探討 NULL 變量如何影響 PowerShell 腳本的不同方面。

對字符串的影響

如果您在字符串中使用 $null,它將是一個空字符串(或空白值)。

$value = $null
Write-Output "The value is $value."

輸出:

The value is .

為了保持腳本的清晰性,建議在使用變量在日誌消息中時將其放在括號中([]),特別是當變量的值位於字符串的末尾時。

$value = $null
Write-Output "The value is [$value]"

輸出:

The value is []

這種做法有助於區分空字符串和 $null 值。

對數值方程的影響

當在數字方程中使用 $null 變量時,如果它們沒有生成錯誤,我們的結果將無效。有時,$null 變量將計算為 0,或者會使整個結果變為 $null

考慮這個涉及乘法的例子,根據值的順序,結果可能是 0$null

$null * 5
$null -eq ( $null * 5 )

輸出:

True

對集合的影響

集合允許我們使用索引來訪問值。如果我們嘗試索引一個 null 集合,我們將收到這個錯誤 "無法索引到 null 數組"

$value = $null
$value[10]

輸出:

Cannot index into a null array.
At line:2 char:1
+ $value[10]
+ ~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

然而,如果您有一個集合並試圖檢索該集合中不存在的元素,您將獲得 $null 的結果。

$array = @( 'one', 'two', 'three' )
$null -eq $array[100]

輸出:

True

對對象的影響

如果我們嘗試訪問一個未指定屬性的對象的屬性或子屬性,我們可以得到一個 $null 變量,就像您對未定義的變量所做的那樣。在這種情況下,值是 $null 還是真實對象都無關緊要。

$null -eq $undefined.not.existing

輸出:

True

對方法的影響

$null 變量上調用一個方法將引發 RuntimeException 異常。

$value = $null
$value.toString()

輸出:

You cannot call a method on a null valued expression.
At line:2 char:1
+ $value.toString()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

每當您遇到短語 "您無法在 null 值表達式上調用方法" 時,這是一個信號,提示您檢查您的代碼,查找在未先檢查 $null 值的情況下調用方法的地方。

在 PowerShell 中檢查 null 值

使用比較運算符 -eq-ne 在 PowerShell 中檢查 null 值

在 PowerShell 中檢查 $null 值時,最佳做法是將 $null 變量放在比較的左側。這種寫作風格不僅是故意的,而且被接受為 PowerShell 的最佳實踐。

然而,在某些情況下,將 $null 放在右側可能會給您意想不到的結果。讓我們看以下代碼並嘗試預測結果:

if ( $value -eq $null ) {
    'The array is $null'
}

if ( $value -ne $null ) {
    'The array is not $null'
}

如果我們不定義 $value,第一個將評估為 $true,我們的消息將是 數組為 $null。此處的警告是,創建一個 $value 使得兩個結果都為 $false 是可能的。

輸出:

The array is $null

在這種情況下,$value 變量是一個包含 $null 的數組。

-eq 運算符檢查數組中的每個值並返回匹配的 $null,它評估為 $false。類似地,-ne 運算符返回所有不匹配 $null 值的結果,導致沒有結果(同樣評估為 $false)。

因此,無論哪一個條件都不是 $true,即使其中一個可能被期望為如此。

使用 -not 運算符在 PowerShell 中檢查 null 值

-not 運算符是 PowerShell 中的一個邏輯運算符,用於否定條件。當應用於變量時,它檢查該變量是否不為 null。

$myVariable = $null

if (-not $myVariable) {
    Write-Host "The variable is null."
}

在這段代碼中,我們有一個初始化為 null 值的變量 $myVariable-not 運算符在 if 語句中用於檢查 $myVariable 是否不為 null。

如果條件為真,則腳本將運行 if 語句中的代碼塊。

輸出:

The variable is null.

如輸出所示,腳本識別了 $myVariable 為 null,並輸出消息 "該變量為 null"

使用 IsNullOrEmpty() 方法在 PowerShell 中檢查 null 值

IsNullOrEmpty()System.String 類的一個方法,而不是本地 PowerShell 方法。不同的數據類型可能有不同的方法來檢查 null 或空值。

在這段代碼中,我們創建一個名為 $myString 的變量並將其賦值為 $null,表示一個 null 或空字符串。

我們使用 [string]::IsNullOrEmpty($myString) 方法來檢查 $myString 是否為 null 或空。這個方法是 .NET Framework 中 [string] 類的一部分,用於確定字符串是否為 null 或空字符串。

如果 $myString 確實為 null 或空,條件為真,腳本將打印 "字符串為 null 或空。"

$myString = $null

if ([string]::IsNullOrEmpty($myString)) {
    Write-Host 'The string is null or empty.'
}

輸出:

The string is null or empty.

由於 $myString 被賦值為 $null,因此條件為真,消息會顯示在輸出中。

使用 IsNullOrWhiteSpace() 方法在 PowerShell 中檢查 null 值

IsNullOrWhiteSpace() 是 .NET Framework 的 System.String 類中提供的一個有用方法,您可以通過在字符串變量上調用它來在 PowerShell 中使用。

此方法檢查字符串是否為 null、空或僅由空格字符組成。當您想檢查一個字符串變量是否實際上是空的,即使它僅包含空格或製表符時,這是非常方便的。

在下面的代碼中,我們創建一個名為 $myString 的變量並將其賦值為僅包含空格字符的字符串。

然後,我們使用 [string]::IsNullOrWhiteSpace($myString) 方法來檢查 $myString 是否為 null 或僅包含空白。

$myString = $null

if ([string]::IsNullOrWhiteSpace($myString)) {
    Write-Host 'The string is null or contains only whitespace.'
}

輸出:

The string is null or contains only whitespace.

由於條件為真($myString 為 null),腳本將打印 "字符串為 null 或僅包含空白。"

這個方法在您想確保字符串實際上是空的時候特別有用,即使它因為空格或製表符而顯得是 "空白"

結論

在 PowerShell 腳本中,了解 $null 變量的工作原理很重要,因為它們可能會影響腳本在不同情況下的行為。為了編寫可靠的腳本,遵循最佳實踐是個好主意,例如確保通過將 $null 放在左側來正確檢查 null 值。

無論您是剛剛開始學習腳本編寫還是已有經驗,了解這些細節將幫助您更好地使用 PowerShell。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

相關文章 - PowerShell Variable