我有一个简单的脚本
Param([string] $myStringValue)
echo $myStringValue
Run Code Online (Sandbox Code Playgroud)
当我打电话给它时./test.ps1 -myStringValue steve,它运作得很好.
但是如果我在开头添加Set-StrictMode:
Set-StrictMode -Version Latest
Param([string] $myStringValue)
echo $myStringValue
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
> ./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
Run Code Online (Sandbox Code Playgroud)
我$myStringValue事先尝试过设置
$myStringValue = ''
Set-StrictMode -Version Latest
Param([string] $myStringValue)
echo $myStringValue
Run Code Online (Sandbox Code Playgroud)
但这只是让它在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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我认为问题在于param()需要首先(或者仅在某些特殊陈述之前).放在块Set-StrictMode后面param().
您可以在ISE中看到这一点,因为语法突出显示param()将从深蓝色(语句)更改为蓝色(cmdlet/function/expression).
| 归档时间: |
|
| 查看次数: |
359 次 |
| 最近记录: |