I tried executing Blah.ps1 as a Powershell job, and that has the
interesting consequence of stalling until you do a Receive-Job
and then it lets you interactively supply the value for $Name
Here's Blah.ps1
function Blah {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String] $Name
)
"Hello $Name"
}
Blah
and here's how I ran it as a job
PS C:> $job = start-job -scriptblock {. "C:\Documents and
Settings\Larry\My Documents\WindowsPowerShell\Blah.ps1"}
PS C:> $job
Id Name State HasMoreData Location
-- ---- ----- ----------- --------
21 Job21 Blocked True localhost
PS C:> $job | Receive-job
cmdlet Blah at command pipeline position 1
Supply values for the following parameters:
Name: Bob
PS C:> $job
Id Name State HasMoreData Location
-- ---- ----- ----------- --------
21 Job21 Completed True localhost
PS C:> $job | Receive-job
Hello Bob
PS C:> $job
Id Name State HasMoreData Location
-- ---- ----- ----------- --------
21 Job21 Completed False localhost
PS C:>
You can see the job's State change from Blocked to Completed, and
the HasMoreDate change from True to False. I typed in "Bob" after
the first Receive-Job command to unblock it.
- Larry
On 5/24/2010 3:13 AM, Chris Dent wrote:
>
> Yeah, it will, Mandatory takes precedence and you're right that
> validation can only take place after the parameter is entered.
>
> If you're calling the function in a script you could always add
> ValidateNotNullOrEmtpy() then call the function with:
>
> Blah -Name $Name
>
> $Name will go through validation since the parameter is present, but
> this isn't the same behaviour as you'd want by requiring a parameter.
>
> Chris
>
> DioGenus wrote:
>> Throw is OK, I just wanted to be updated on the new ways of doing
>> things. The new parameter attribute-approcah seems so holistic and
>> complete that I suspected that Microsoft had introduced an
>> "attribute-way" that replaced the throw-technique, e.g. a
>> [Parameter(Prompt=$false)] statement. Could not find anything in the
>> documentation, though...
>>
>> BTW: I have already tried [ValidateNotNullOrEmpty()] in combination
>> with Mandatory and the prompt still appears. I believe validation
>> takes place after data is returned by the prompt.
>>
>> Dio Genus
>>
>> "Chris Dent" wrote:
>>
>>> I haven't found a way.
>>>
>>> You could use [ValidateNotNullOrEmpty()], however that's implicit
>>> with Mandatory, and ineffective without (unless the parameter is
>>> actually supplied).
>>>
>>> Is throw so bad?
>>>
>>> Chris
>>>
>>> DioGenus wrote:
>>>> Is there a way to turn of this prompting for missing mandatory
>>>> parameters and instead throw an error (besides using a
>>>> throw-statement as the parameter default value)? Getting a prompt on
>>>> the console is not very useful in batch jobs.
>>>>
>>>> Dio Genus
>>> .
>>>