Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Catch ParameterBindingException

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-11-2007   #1 (permalink)
Manfred
Guest


 

Catch ParameterBindingException

Hello,

I got the following problem. I created a function and want to pass a
parameter, lets call it e. I know how to add this like:

function xyz([boolean]$e=$false)
{
if ($e) {Write-Host "E was set");
}

everything is OK when I call it like that: xyz -e 1 or xyz -e $true

But what if I just want to know if the parameter is present and do not
want to pass a value like: xyz -e -> This should produce the same output
as above.

Everytime I carry out the last command, it throws a
ParameterBindingException. The problem is that I cannot trap this
exception in the function itself.

Anybody an idea what to do ? My last solution was to handle the
parameters by myself. But I hope there is a solution to do this like the
above function in a way.

Manfred

My System SpecsSystem Spec
Old 01-11-2007   #2 (permalink)
RichS
Guest


 

RE: Catch ParameterBindingException

does this do what you want?

function xyz
{
param ([boolean] $e=$false)
if ($e) {Write-Host "E was set"}
else {Write-Host "E was NOT set"}
}


--
Richard Siddaway

Please note that all scripts are supplied "as is" and with no warranty


"Manfred" wrote:

> Hello,
>
> I got the following problem. I created a function and want to pass a
> parameter, lets call it e. I know how to add this like:
>
> function xyz([boolean]$e=$false)
> {
> if ($e) {Write-Host "E was set");
> }
>
> everything is OK when I call it like that: xyz -e 1 or xyz -e $true
>
> But what if I just want to know if the parameter is present and do not
> want to pass a value like: xyz -e -> This should produce the same output
> as above.
>
> Everytime I carry out the last command, it throws a
> ParameterBindingException. The problem is that I cannot trap this
> exception in the function itself.
>
> Anybody an idea what to do ? My last solution was to handle the
> parameters by myself. But I hope there is a solution to do this like the
> above function in a way.
>
> Manfred
>

My System SpecsSystem Spec
Old 01-11-2007   #3 (permalink)
Jeffrey Snover [MSFT]
Guest


 

Re: Catch ParameterBindingException

It sounded like he was looking for SWITCH. Does this do what you want:

function xyz([Switch]$e)
{
if ($e) {Write-Host "E was Set"}
else {Write-Host "E was NOT set"}
}

PS> xyz -e
E was Set
PS> xyz
E was NOT set
PS> xyz -e:$true
E was Set
PS> xyz -e:$false
E was NOT set
PS> xyz -e:1
--
Jeffrey Snover [MSFT]
Windows PowerShell Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Try Catch Brian .NET General 2 04-12-2008 12:48 PM
catch-up John A Grandy Vista mail 0 03-02-2008 07:53 PM
Catch-all Greg23 PowerShell 4 08-14-2007 02:18 AM
How to TRY and CATCH John Smith PowerShell 3 12-22-2006 01:55 AM
I Catch VI Jason Vista hardware & devices 0 10-03-2006 07:01 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51