共用方式為


Set-StrictMode

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

語法

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

Description

Set-StrictMode Cmdlet 會設定目前範圍和所有子範圍的嚴格模式,並開啟和關閉它。 當 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

The variable $a cannot be retrieved because it has not been set yet.

At line:1 char:3
+ $a <<<<  -gt 5
+ CategoryInfo          : InvalidOperation: (a:Token) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined

將 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)

The function or command was called like a method. Parameters should be separated by spaces,
as described in 'Get-Help about_Parameter.'
At line:1 char:4
+ add <<<< (3,4)
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : StrictModeFunctionCallWithParens

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

True

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

Property 'Month' cannot be found on this object; make sure it exists.
At line:1 char:9
+ $string. <<<< month
+ CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFoundStrict

此命令會開啟 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)
$a[2] -eq $null
$a['abc'] -eq $null

True
True

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

Index was outside the bounds of the array.
At line:1 char:1
+ $a[2] -eq $null
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException

Cannot convert value "abc" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ $a['abc'] -eq $null
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

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

參數

-Off

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

類型:SwitchParameter
Position:Named
預設值:None
必要:True
接受管線輸入:False
接受萬用字元:False

-Version

指定在 strict 模式中造成錯誤的條件。 此參數接受任何有效的 PowerShell 版本號碼。 任何高於 3 的數字都會被視為 Latest

此參數的有效值為:

  • 1.0
    • 禁止參考未初始化的變數,但字串中未初始化的變數除外。
  • 2.0
    • 禁止參考未初始化的變數。 這包括字串中未初始化的變數。
    • 禁止參考物件不存在的屬性。
    • 禁止使用呼叫方法語法的函式呼叫。
  • 3.0
    • 禁止參考未初始化的變數。 這包括字串中未初始化的變數。
    • 禁止參考物件不存在的屬性。
    • 禁止使用呼叫方法語法的函式呼叫。
    • 禁止超出界限或無法解析的陣列索引。
  • 最近的
    • 選取可用的最新版本。 最新版本是最嚴格的版本。 使用此值可確保腳本使用最嚴格的可用版本,即使將新版本新增至 PowerShell 也一定。
類型:Version
別名:v
Position:Named
預設值:None
必要:True
接受管線輸入:False
接受萬用字元:False

輸入

None

您無法使用管線將輸入傳送至此 Cmdlet。

輸出

None

此 Cmdlet 不會傳回任何輸出。

備註

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