Question:
I’m making a call to a powershell script:
1 2 |
powershell.exe -file basic.ps1 -User "" |
I have other parameters, this is a bit watered down.
The PS script accepts the params :
1 2 3 4 5 6 7 |
[CmdLetBinding()] param( [Parameter(Mandatory=$true)] [AllowEmptyString()] [string]$User = 't' ) |
When I run command i get:
Missing an argument for parameter ‘User’. Specify a parameter of type ‘System.String’ and try again.
I was under the assumption that AllowEmptyString
would allow this?
Answer:
It doesn’t sound like you want a mandatory parameter at all. Making it mandatory will require input which makes the default value useless. However, let’s get back to the error your described.
The attribute works as expected. The problem is how you’re calling your script. Lets try the following code as a function and a script:
1 2 3 4 5 6 7 8 9 |
[CmdLetBinding()] param( [Parameter(Mandatory=$true)] [AllowEmptyString()] [string]$User = 't' ) "`$User has value '$user'. Is it null? $($user -eq $null). Type is $($user.GetType().Name)" |
Demo:
1 2 3 4 5 6 7 8 9 10 11 12 |
#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 |
However, when you run the last line in a PowerShell-session, then PowerShell will parse the string which results in an empty value (no quotes) for the parameter.
1 2 3 4 5 6 |
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 is not intended to be used inside a PowerShell-process. You can fix this by calling scripts directly inside a PowerShell-process (second example), run PowerShell.exe …
from cmd/run/scheduled task ++ (anywhere else) or by making sure PS doesn’t parse your arguments so it will actually input the quotes.
This can be done by escaping the quotes
1 2 |
PS> powershell -File Untitled-5.ps1 -User `"`" |
Or by using
--%
1 2 |
PS> powershell -File Untitled-5.ps1 --% -User "" |