2

在我的 PowerShell 配置文件中,我有Set-StrictMode -Version 2.0. 不幸的是,我有一些脚本(和模块)在启用严格模式时无法正常工作。

我试图通过将其放在Set-StrictMode -Off脚本的顶部或使用行为不端的模块的脚本的顶部来解决此问题。不幸的是,它似乎没有任何效果。

如何在 PowerShell 中临时禁用严格模式?

更多细节:这特别发生在 PsGet 中。见问题 57。我Set-StrictMode -Version 2.0在我的个人资料中有。如果我尝试Install-Module从脚本中使用 PsGet,我会收到错误消息“在此对象上找不到属性‘动词’”。

即使我放在Set-StrictMode -Off脚本的顶部也会发生这种情况。如果我Set-StrictMode -Off在运行脚本之前在命令行运行,则不会出现错误并且脚本运行良好。

在打开严格模式之前,PsGet 已导入到我的配置文件中。

4

1 回答 1

1

就像马特在评论中说的那样,我没有看到这个问题重现。你能提供更多细节吗?

内容StrictModeTest.ps1

Set-StrictMode -Off

# variable $x doesn't exist, should trigger strictmode error
"Value is [$($x.Path)]"

测试(PSv3):

PS C:\> Set-StrictMode -Version 2.0
PS C:\> .\StrictModeTest.ps1
Value is []

如果我注释掉该Set-StrictMode -Off行,我会收到预期的错误:

PS C:\> .\StrictModeTest.ps1
The variable '$x' cannot be retrieved because it has not been set.
At C:\StrictModeTest.ps1:4 char:14
+ "Value is [$($x.Path)]"
+              ~~
    + CategoryInfo          : InvalidOperation: (x:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

Value is []
于 2013-02-26T20:58:14.560 回答