INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Validate a param

Validate a param

(OP)
Hi I am using a parameter called foo which I set to mandatory=$false. This means it is not required. In my script I would like to test if it exists or not and do something if it does.


For example if someone runs .\myscripts.ps1 -FOO valueisgiven the scripts will do something if not just ignore. Should I do something like if ($FOO -ne "NULL") { do something} else .....

Thank you.

CODE --> powershell

[Parameter(Mandatory=$false,Position=3)]
 [String]
 $FOO 


CODE --> powershell

if ($FOO -ne "NULL") 
{
do sometthing
} else {
do something else
}[/indent]




RE: Validate a param

Assuming you're doing validation checks on the $foo variable first, I think you could do:

CODE -->

if ($FOO) 
{
  do sometthing
}
else
{
  do something else
} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Validate a param

Just to clarify my comment about validation checking the variable, I would make sure it isn't a bunch of spaces, first, unless that is an acceptable entry.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Validate a param

(OP)
Hi Blister this did not work.

I have also tried

CODE -->

if (!($foo -eq $NULL))
{
  first
}
else
{
  second
} 


however it still returns me to the first if statement regardless if I run my script with parameter or not.

Example:

.\myscript.ps1 $foo "hello"

or

.\myscript.ps1

I get same results.

RE: Validate a param

cdtech1980,

What does your script look like?
You need to have something like this as the first line:
param([string]$foo = "")

Then the If-statement

You would then call your script one of these ways:
.\myscript.ps1 -foo "hello" (if using a variable)
.\myscript.ps1 (no variable)

script example

CODE -->

param([string]$foo = "")
if ($foo) 
{
  do something
}
else
{
  do something else
} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Validate a param

...one more thing.

If $foo is required, you'd have to specify that it's mandatory on the param line. You can also specify the position
so that you can call the script without adding the -foo switch

I think it goes like this:

CODE -->

param ([Parameter(Mandatory=$TRUE,Position=1)][string]$foo="") 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Validate a param

(OP)
Hi I have this up top. I do not want it to be mandatory. If the user does not have -foo I want the script to continue to the else statement.

CODE -->

param(
        [Parameter(Mandatory=$false,Position=0)]
        [ValidateNotNullOrEmpty()]
        [String]
        $foo

) 

Even just tried

CODE -->

param(
        [Parameter(Mandatory=$false,Position=0)]
        [ValidateNotNullOrEmpty()]
        [String]
        $foo = ""

) 

Still goes to the else even if the user does not enter -foo "hello"

RE: Validate a param

Here is my myscript.ps1:

CODE -->

param([Parameter(Position=1)][string]$foo = "")
if ($foo) 
{
  write-host "Foo found."
}
else
{
  write-host "No Foo"
} 

Here are my results:

CODE

[PS] C:\Temp>.\myscript.ps1
No Foo

[PS] C:\Temp>.\myscript.ps1 "hello"
Foo found.

[PS] C:\Temp>.\myscript.ps1 -foo "hello"
Foo found.

[PS] C:\Temp>.\myscript.ps1 -foo ""
No Foo

[PS] C:\Temp>.\myscript.ps1 ""
No Foo 

I hope this helps out.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Validate a param

(OP)
Let me get rid of mandatory =false.
Thanks for the results.

RE: Validate a param

(OP)
It works. Thanks man!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close