Set-StrictMode
在表达式、脚本和脚本块中建立并强制实施编码规则。
语法
Version (默认值)
Set-StrictMode
-Version <Version>
[<CommonParameters>]
Off
Set-StrictMode
[-Off]
[<CommonParameters>]
说明
Set-StrictMode cmdlet 为当前范围和所有子范围配置严格模式,并打开和关闭它。 当严格模式处于打开状态时,当表达式、脚本或脚本块的内容违反基本最佳做法编码规则时,PowerShell 将生成终止错误。
使用 Version 参数来确定强制执行哪些编码规则。
Set-PSDebug -Strict cmdlet 为全局范围启用严格模式。
Set-StrictMode 仅影响当前范围及其子范围。 因此,可以在脚本或函数中使用它来替代从全局范围继承的设置。
Set-StrictMode 关闭时,PowerShell 具有以下行为:
- 假定未初始化变量的值为 0(零)或
$Null,具体取决于类型 - 对不存在的属性的引用返回
$Null - 错误函数语法的结果因错误条件而异
- 尝试在数组中使用无效索引检索值将返回
$Null
示例
示例 1:以版本 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
如果严格模式设置为版本 1.0,则尝试引用未初始化的变量失败。
示例 2:以版本 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
此命令打开严格模式并将其设置为版本 2.0。 因此,如果对函数调用或引用未初始化的变量或不存在的属性使用括号和逗号的方法语法,PowerShell 将返回错误。
示例输出显示版本 2.0 严格模式的效果。
如果没有版本 2.0 严格模式,“(3,4)”值将解释为一个数组对象,其中未添加任何内容。 通过使用版本 2.0 严格模式,正确将其解释为提交两个值的错误语法。
如果没有版本 2.0,则对字符串不存在的 Month 属性的引用仅返回 $Null。 通过使用版本 2.0,它被正确解释为引用错误。
示例 3:以版本 3.0 启用严格模式
如果严格模式设置为 关闭,则无效或超出边界索引的结果返回 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 对当前作用域和所有子范围关闭严格模式。
参数属性
| 类型: | SwitchParameter |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
Off
| Position: | Named |
| 必需: | True |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-Version
指定在严格模式下导致错误的条件。 此参数接受任何有效的 PowerShell 版本号。 任何高于 3 的数字都被视为 最新。
此参数的有效值为:
- 1.0
- 禁止引用未初始化的变量,字符串中未初始化的变量除外。
- 2.0
- 禁止引用未初始化的变量。 这包括字符串中的未初始化变量。
- 禁止引用对象不存在的属性。
- 禁止使用用于调用方法的语法的函数调用。
- 3.0
- 禁止引用未初始化的变量。 这包括字符串中的未初始化变量。
- 禁止引用对象不存在的属性。
- 禁止使用用于调用方法的语法的函数调用。
- 禁止超出边界或无法解决的数组索引。
- 最晚
- 选择可用的最新版本。 最新版本最严格。 使用此值可确保脚本使用最严格的可用版本,即使将新版本添加到 PowerShell 也是如此。
参数属性
| 类型: | Version |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
| 别名: | v |
参数集
Version
| Position: | Named |
| 必需: | True |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
CommonParameters
此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters。
输入
None
不能通过管道将输入传递给此 cmdlet。
输出
None
此 cmdlet 不返回任何输出。
备注
Set-StrictMode 仅在其设置范围及其子范围中有效。 有关 PowerShell 中的作用域的详细信息,请参阅 about_Scopes。