![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
![]() |
| 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 |