2017-02-24 16 views
2

私は単純なスクリプトSet-StrictModeはパラメータブロックと互換性がありませんか?

Param([string] $myStringValue) 
echo $myStringValue 

を持っていると私は./test.ps1 -myStringValue steveとそれを呼び出すときに、それだけで正常に動作します。

しかし、私が最初に設定-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 

私は間違っていますか?

+0

おそらく、パラメータを必須としてマークするか、何かに初期化することができます。 –

答えて

7

私は、param()が最初である(または特定の特殊な文が先行する)必要があると考えています。 param()ブロックの後ろにSet-StrictModeを置きます。

構文の強調表示が暗い(文)から青(コマンドレット/関数/式)に変更されるため、ISEで確認できます。

関連する問題