syntax question on the throw statement

F

Flea

I was wondering if anyone could explain the syntaxt of the throw statement?
For example, I have the following script:

param
(
[string] $importantParameter = $(throw "Please specify the important
parameter!")
)

What is the significance of putting this statement within the $ () ? What
does the $ represent here?

Thanks,
Flea
 

My Computer

K

Kiron

$( ) is a subexpression. The syntax $(throw <String>) is used in param definitions when you want the parameter to be mandatory. If the Throw statement is not evaluated inside a subexpression, the parser complains when the statement is on the right side of the assignment operator.

# parser complains
function f1 ($x = throw 'oops!') {$x}

# -x is mandatory
function f1 ($x = $(throw 'oops!')) {$x}
f1 hi
fi

Subexpression are also good for assigning the output of conditional statements, Switch, Foreach, While and For loops to variables. Also, sometimes you need to pipe output from Switch, Foreach, While and For loops, wrapping them in a subexpression does push their output down the pipeline.

--
Kiron
 

My Computer

F

Flea

Thanks Kiron!

"Kiron" wrote:

> $( ) is a subexpression. The syntax $(throw <String>) is used in param
> definitions when you want the parameter to be mandatory. If the Throw
> statement is not evaluated inside a subexpression, the parser complains
> when the statement is on the right side of the assignment operator.
>
> # parser complains
> function f1 ($x = throw 'oops!') {$x}
>
> # -x is mandatory
> function f1 ($x = $(throw 'oops!')) {$x}
> f1 hi
> fi
>
> Subexpression are also good for assigning the output of conditional
> statements, Switch, Foreach, While and For loops to variables. Also,
> sometimes you need to pipe output from Switch, Foreach, While and For
> loops, wrapping them in a subexpression does push their output down the
> pipeline.
>
> --
> Kiron
>
 

My Computer

J

Josh Einstein

It's worth mentioning also that in PowerShell 2.0 you can simply mark the
parameter as mandatory.

function Blah {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String] $Name
)

"Hello $Name"

}

Blah "Jerry"
Blah "Newman"
Blah # prompts for name

Josh

"Kiron" <[email protected]> wrote in message
news:[email protected]

> $( ) is a subexpression. The syntax $(throw <String>) is used in param
> definitions when you want the parameter to be mandatory. If the Throw
> statement is not evaluated inside a subexpression, the parser complains
> when the statement is on the right side of the assignment operator.
>
> # parser complains
> function f1 ($x = throw 'oops!') {$x}
>
> # -x is mandatory
> function f1 ($x = $(throw 'oops!')) {$x}
> f1 hi
> fi
>
> Subexpression are also good for assigning the output of conditional
> statements, Switch, Foreach, While and For loops to variables. Also,
> sometimes you need to pipe output from Switch, Foreach, While and For
> loops, wrapping them in a subexpression does push their output down the
> pipeline.
>
> --
> Kiron
 

My Computer

K

Kiron

Yes, good point Josh. Also, in the ISE the HelpMessage attribute provides the message for the prompt; in the console you have to type !? to view the message:

function Blah {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,
HelpMessage='Who do yo want to Blah to?')]
[String] $Name
)

"Hello $Name"

}

Blah Jerry
Blah Newman
Blah # prompts for name


--
Kiron
 

My Computer

J

Josh Einstein

Which I always found a bit redundant... Why can't it just use the
documentation from the comments? I gotta admit I always took the lazy way
out with their help file xml when I was making pssnapins in c# because it
couldn't make use of the c# doc comments. But thanks for reminding me. I
haven't been using HelpMessage in my modules and should probably start.

Josh

"Kiron" <[email protected]> wrote in message
news:[email protected]

> Yes, good point Josh. Also, in the ISE the HelpMessage attribute provides
> the message for the prompt; in the console you have to type !? to view the
> message:
>
> function Blah {
> [CmdletBinding()]
> param(
> [Parameter(Mandatory=$true,
> HelpMessage='Who do yo want to Blah to?')]
> [String] $Name
> )
>
> "Hello $Name"
>
> }
>
> Blah Jerry
> Blah Newman
> Blah # prompts for name
>
>
> --
> Kiron
 

My Computer

D

DioGenus

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

"Josh Einstein" wrote:

> It's worth mentioning also that in PowerShell 2.0 you can simply mark the
> parameter as mandatory.
>
> function Blah {
> [CmdletBinding()]
> param(
> [Parameter(Mandatory=$true)]
> [String] $Name
> )
>
> "Hello $Name"
>
> }
>
> Blah "Jerry"
> Blah "Newman"
> Blah # prompts for name
>
> Josh
>
> "Kiron" <[email protected]> wrote in message
> news:[email protected]

> > $( ) is a subexpression. The syntax $(throw <String>) is used in param
> > definitions when you want the parameter to be mandatory. If the Throw
> > statement is not evaluated inside a subexpression, the parser complains
> > when the statement is on the right side of the assignment operator.
> >
> > # parser complains
> > function f1 ($x = throw 'oops!') {$x}
> >
> > # -x is mandatory
> > function f1 ($x = $(throw 'oops!')) {$x}
> > f1 hi
> > fi
> >
> > Subexpression are also good for assigning the output of conditional
> > statements, Switch, Foreach, While and For loops to variables. Also,
> > sometimes you need to pipe output from Switch, Foreach, While and For
> > loops, wrapping them in a subexpression does push their output down the
> > pipeline.
> >
> > --
> > Kiron
>
>
 

My Computer

C

Chris Dent

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
 

My Computer

D

DioGenus

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
> .
>
 

My Computer

C

Chris Dent

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
>>>
>> .
>>
>>
 

My Computer

L

Larry__Weiss

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
>>> .
>>>
 

My Computer

Top