在運算式、指令碼和指令碼區塊中建立並強制施行程式碼規則。

語法

Set-StrictMode -Off [<CommonParameters>]

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

描述

Set-StrictMode Cmdlet 會為目前範圍 (與所有子範圍) 設定嚴格模式,並開啟及關閉它。開啟嚴格模式後,當運算式、指令碼或指令碼區塊違反基本的最佳程式碼規則時,Windows PowerShell 將會產生終止錯誤。

請使用 Version 參數來判斷已強制施行哪些程式碼規則。

Set-PSDebug Cmdlet 不同,Set-StrictMode 只會影響目前的範圍及其子範圍,因此您可以在指令碼或函數中使用它,而不會影響全域範圍。

當 Set-StrictMode 關閉時,則會假設尚未初始化之變數 (第 1 版) 的值為 0 (零) 或 $null (視類型而定)。若參考不存在的屬性則傳回 $null,而且無效函數語法的結果則會隨著錯誤而不同。不允許使用未具名的變數。

參數

-Off

關閉嚴格模式。此參數也會關閉 "Set-PSDebug -Strict"。

必要?

true

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Version <Version>

指定在嚴格模式中導致錯誤發生的狀況。此參數為必要項。

有效的值包括 "1.0"、"2.0" 和 "Latest"。下列清單會顯示每個值的作用。

1.0

-- 禁止參考尚未初始化的變數 (字串中尚未初始化的變數除外)。

2.0

-- 禁止參考尚未初始化的變數 (包括字串中尚未初始化的變數)。

-- 禁止參考不存在的物件屬性。

-- 禁止使用用來呼叫方法之語法的函數呼叫。

-- 禁止沒有名稱的變數 (${})。

Latest:

-- 選取可用的最新 (最嚴格) 版本。請使用這個值確保指令碼會使用最嚴格的可用版本,即使新版本已新增至 Windows PowerShell 也不例外。

必要?

true

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

None

您無法經由管道將輸入輸出至這個 Cmdlet。

輸出

這個 Cmdlet 不會傳回任何輸出。

附註

Set-StrictMode 類似於 Set-PSDebug 的 Strict 參數。"Set-Strictmode -version 1" 的作用等同於 "Set-PSDebug -strict",唯一的不同是 Set-PSDebug 對所有的範圍都有效,而 Set-StrictMode 只對其設定範圍及其子範圍有效。如需 Windows PowerShell 中範圍的詳細資訊,請參閱 about_Scopes。

範例 1

C:\PS>set-strictmode -version 1.0

C:\PS> $a -gt 5
False
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

描述
-----------
這個命令會開啟嚴格模式,並將它設定為 1.0 版。因此,嘗試參考未初始化的變數將會失敗。

範例輸出會顯示 1.0 版嚴格模式的效果。






範例 2

C:\PS># set-strictmode -version 2.0

# Strict mode is off by default.

C:\PS> function add ($a, $b) {$a + $b}
C:\PS> add 3 4
7
C:\PS> add(3,4)
3
4

C:\PS> set-strictmode -version 2.0

C:\PS> 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


C:\PS> set-strictmode -off

C:\PS> $string = "This is a string".
C:\PS> $string.Month
C:\PS>

C:\PS> set-strictmode -version 2.0

C:\PS> $string = "This is a string".
C:\PS> $string.Month
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

描述
-----------
這個命令會開啟嚴格模式,並將它設定為 2.0 版。因此,如果您將方法語法 (括號和逗號) 用於函數呼叫,或是參考未初始化的變數或不存在的屬性,Windows PowerShell 便會擲回錯誤。

範例輸出會顯示 2.0 版嚴格模式的效果。

若未指定 2.0 版嚴格模式,值 "(3,4)" 會解譯為未加入任何項目的單一陣列物件。若指定 2.0 版嚴格模式,則會正確解譯為提交兩個值的錯誤語法。

若未指定 2.0 版,參考不存在的字串屬性 Month 時只會傳回 null。若指定 2.0 版,便會正確解譯為參考錯誤。






請參閱




目錄