Set-StrictMode是否与参数块不兼容?

时间:2017-02-24 20:20:08

标签: powershell

我有一个简单的脚本

Param([string] $myStringValue)
echo $myStringValue

当我用./test.ps1 -myStringValue steve调用它时,它的工作正常。

但是如果我将Set-StrictMode添加到开头:

Set-StrictMode -Version Latest
Param([string] $myStringValue)
echo $myStringValue

我收到以下错误:

> ./test.ps1 -myStringValue steve
The variable '$myStringValue' cannot be retrieved because it has not been set.
At D:\code\cadgraphics\test.ps1:2 char:20
+     Param([string] $myStringValue)
+                    ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (myStringValue:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

The variable '$myStringValue' cannot be retrieved because it has not been set.
At D:\code\cadgraphics\test.ps1:3 char:10
+     echo $myStringValue
+          ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (myStringValue:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

我尝试事先设置$myStringValue

$myStringValue = ''
Set-StrictMode -Version Latest
Param([string] $myStringValue)
echo $myStringValue

但这只是让它在Param区块上窒息:

Param : The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At D:\code\cadgraphics\test.ps1:3 char:5
+     Param([string] $myStringValue)
+     ~~~~~
    + CategoryInfo          : ObjectNotFound: (Param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我做错了什么?

1 个答案:

答案 0 :(得分:9)

我认为问题在于<table> <tr> <td><iframe src="http://example.com/"></iframe></td> <td><iframe src="http://example.com/"></iframe></td> </tr> <tr> <td><iframe src="http://example.com/"></iframe></td> <td><iframe src="http://example.com/"></iframe></td> </tr> </table> 需要首先(或者仅在某些特殊陈述之前)。将param()放在Set-StrictMode块之后。

您可以在ISE中看到这一点,因为语法突出显示会将param()从深蓝色(语句)更改为蓝色(cmdlet / function / expression)。