共用方式為


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︰開啟 strict 模式為 1.0 版

# 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︰開啟 strict 模式為 2.0 版

# 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

將嚴格模式設定為第 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

此參數的有效值為:

  • 1.0
    • 禁止參考未初始化的變數,但字串中未初始化的變數除外。
  • 2.0
    • 禁止參考未初始化的變數。 這包括字串中未初始化的變數。
    • 禁止參考物件不存在的屬性。
    • 禁止使用語法呼叫方法的函式呼叫。
  • 3.0
    • 禁止參考未初始化的變數。 這包括字串中未初始化的變數。
    • 禁止參考物件不存在的屬性。
    • 禁止使用語法呼叫方法的函式呼叫。
    • 禁止超出界限或無法解析的陣列索引。
  • 最新
    • 選取可用的最新版本。 最新的版本是最嚴格的。 使用此值可確保腳本使用最嚴格的可用版本,即使新版本新增至 PowerShell 也是如此。
Type:Version
Aliases:v
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

輸入

None

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

輸出

None

此 Cmdlet 不會傳回任何輸出。

備註

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