Set-StrictMode

在表達式、腳本和腳本區塊中建立並強制執行編碼規則。

Syntax

Set-StrictMode
   -Version <Version>
   [<CommonParameters>]
Set-StrictMode
   [-Off]
   [<CommonParameters>]

Description

Cmdlet 會 Set-StrictMode 設定目前範圍和所有子範圍的嚴格模式,並開啟和關閉它。 當 strict 模式開啟時,當表達式、腳本或腳本區塊的內容違反基本最佳做法編碼規則時,PowerShell 會產生終止錯誤。

使用 Version 參數來判斷要強制執行的編碼規則。

Set-PSDebug -Strict Cmdlet 會開啟全域範圍的嚴格模式。 Set-StrictMode 只會影響目前的範圍及其子範圍。 然後,您可以在腳本或函式中使用它來覆寫繼承自全域範圍的設定。

關閉時 Set-StrictMode ,PowerShell 具有下列行為:

  • 根據類型,假設未初始化的變數值為 0 (零) 或 $Null
  • 不存在屬性的參考會傳回 $Null
  • 不正確的函式語法結果會隨著錯誤狀況而有所不同
  • 嘗試使用陣列中無效的索引擷取值會傳回 $Null

範例

範例 1:以 1.0 版的身分開啟 strict 模式

# Strict mode is off by default.
$a -gt 5

False

Set-StrictMode -Version 1.0
$a -gt 5

InvalidOperation: The variable '$a' cannot be retrieved because it has not been set.

將 strict 模式設定為 版本 1.0時,嘗試參考未初始化的變數會失敗。

範例 2:以 2.0 版的身分開啟 strict 模式

# Strict mode is off by default.
function add ($a, $b) {
    '$a = ' + $a
    '$b = ' + $b
    '$a+$b = ' + ($a + $b)
}
add 3 4

$a = 3
$b = 4
$a+$b = 7

add(3,4)

$a = 3 4
$b =
$a+$b = 3 4

Set-StrictMode -Version 2.0
add(3,4)

InvalidOperation: The function or command was called as if it were a method. Parameters should be separated by spaces. For information about parameters, see the about_Parameters Help topic.

Set-StrictMode -Off
$string = "This is a string."
$null -eq $string.Month

True

Set-StrictMode -Version 2.0
$string = "This is a string."
$null -eq $string.Month

PropertyNotFoundException: The property 'Month' cannot be found on this object. Verify that the property exists.

這個指令會開啟 strict 模式,並將它設定為版本 2.0。 因此,如果您針對函數調用或參考未初始化的變數或不存在的屬性,使用括號和逗號的方法語法,PowerShell 會傳回錯誤。

範例輸出會顯示版本 2.0 strict 模式的效果。

若沒有版本 2.0 strict 模式,值 (3,4) 會解譯為單一陣列物件,但不會新增任何內容。 使用版本 2.0 strict 模式時,它會正確地解譯為提交兩個值的錯誤語法。

如果沒有版本2.0,字串不存在之 Month 屬性的參考只會$Null傳回 。 使用 版本 2.0時,它會正確地解譯為參考錯誤。

範例 3:以 3.0 版的身分開啟 strict 模式

將 strict 模式設定為 Off,無效或超出界限的索引結果會傳回 Null 值。

# Strict mode is off by default.
$a = @(1)
$null -eq $a[2]
$null -eq $a['abc']

True
True

Set-StrictMode -Version 3.0
$a = @(1)
$null -eq $a[2]
$null -eq $a['abc']

OperationStopped: Index was outside the bounds of the array.

InvalidArgument: Cannot convert value "abc" to type "System.Int32". Error: "Input string was not in a correct format."

將 strict 模式設定為版本或更新版本 3 時,無效或超出界限的索引會導致錯誤。

參數

-Off

表示此 Cmdlet 會關閉目前範圍和所有子範圍的嚴格模式。

Type:SwitchParameter
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-Version

指定在 strict 模式中造成錯誤的條件。 此參數接受任何有效的 PowerShell 版本號碼。 任何高於 3 的數字都會 Latest被視為 。 提供的值必須是字串,或是可以轉換成 System.Version 類型的字串Latest。 版本必須符合有效的 PowerShell 版本。

此參數的有效值為:

  • 1.0
    • 禁止參考未初始化的變數,但字串中未初始化的變數除外。
  • 2.0
    • 禁止參考未初始化的變數。 這包括字串中未初始化的變數。
    • 禁止參考物件不存在的屬性。
    • 禁止使用呼叫方法語法的函式呼叫。
  • 3.0
    • 禁止參考未初始化的變數。 這包括字串中未初始化的變數。
    • 禁止參考物件不存在的屬性。
    • 禁止使用呼叫方法語法的函式呼叫。
    • 禁止超出界限或無法解析的陣列索引。
  • Latest
    • 選取可用的最新版本。 最新版本是最嚴格的版本。 使用此值可確保腳本使用最嚴格的可用版本,即使將新版本新增至 PowerShell 也一定。

警告

Latest腳本中使用 Version 並不具決定性。 在新的 PowerShell 版本中,的意義 Latest 可能會變更。 針對舊版 PowerShell 撰寫的腳本,在 Set-StrictMode -Version Latest 較新版本的 PowerShell 中執行時,會受限於更嚴格的規則。

Type:Version
Aliases:v
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

輸入

None

您無法使用管線將物件傳送至此 Cmdlet。

輸出

None

此 Cmdlet 不會傳回任何輸出。

備註

雖然 Version 參數接受的值大於 3.0,但沒有針對高於3.0的任何項目定義其他規則。

Set-StrictMode 只有在其設定於 其子範圍的範圍中才有效。 如需 PowerShell 中範圍的詳細資訊,請參閱 about_Scopes