PowerShellスクリプトを呼び出しています。
powershell.exe -file basic.ps1 -User ""
私は他のパラメータを持っています、これは少し骨抜きにされています。
PSスクリプトはパラメータを受け入れます:
[CmdLetBinding()]
param(
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]$User = 't'
)
コマンドを実行すると、次のようになります。
パラメータ 'User'の引数がありません。タイプ 'System.String'のパラメーターを指定して、再試行してください。
私はAllowEmptyString
これを可能にするだろうという仮定の下にありましたか?
必須のパラメータが必要なようには思えません。必須にする場合は、入力が必要になり、デフォルト値が使用できなくなります。ただし、説明したエラーに戻りましょう。
属性は期待どおりに機能します。問題は、スクリプトをどのように呼び出すかです。次のコードを関数およびスクリプトとして試してみましょう。
[CmdLetBinding()]
param(
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]$User = 't'
)
"`$User has value '$user'. Is it null? $($user -eq $null). Type is $($user.GetType().Name)"
デモ:
#Inside a function
PS> t -User ""
$User has value ''. Is it null? False. Type is String
#Inside a script
PS> .\Untitled-5.ps1 -User ""
$User has value ''. Is it null? False. Type is String
#Running the following command from cmd
cmd> powershell.exe -file Untitled-5.ps1 -User ""
$User has value ''. Is it null? False. Type is String
ただし、PowerShellセッションの最後の行を実行すると、PowerShellは文字列を解析するため、パラメーターの値は空(引用符なし)になります。
PS> powershell.exe -file Untitled-5.ps1 -User ""
D:\Downloads\Untitled-5.ps1 : Missing an argument for parameter 'User'. Specify a parameter of type 'System.String' and
try again.
+ CategoryInfo : InvalidArgument: (:) [Untitled-5.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument,Untitled-5.ps1
Powershell.exeは、PowerShellプロセス内で使用するためのものではありません。これを修正するには、PowerShellプロセス内で直接スクリプトを呼び出すか(2番目の例)、PowerShell.exe …
cmd / run / cheduled task ++から実行するか(他の場所)、PSが引数を解析しないようにして、実際に引用符を入力します。 。
これは、引用符をエスケープすることで実行できます
PS> powershell -File Untitled-5.ps1 -User `"`"
またはを使用して --%
PS> powershell -File Untitled-5.ps1 --% -User ""
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加