2013-02-26 68 views
2

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

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

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

的更多细节:此具体地说PsGet发生。见issue 57。我的个人资料中有Set-StrictMode -Version 2.0。如果我尝试从脚本中使用PsGet的Install-Module,则会出现错误,指出在此对象上找不到“属性”动词。“

即使将Set-StrictMode -Off放在脚本的顶部,也会发生这种情况。如果我在运行脚本之前在命令行运行Set-StrictMode -Off,那么我不会收到任何错误,并且脚本可以正常工作。

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

+0

确定它是导致错误发生的Set-StrictMode?我猜你已经从$ profile中删除了相同的脚本了吗?我一直在试图复制这个问题,但我总是可以通过在脚本或控制台中显式调用它来覆盖我的$ profile。 – Matt 2013-02-26 18:54:48

+0

我在问题中添加了更多细节。 – 2013-02-27 10:37:28

回答

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 []