Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate a param 1

Status
Not open for further replies.
Nov 26, 2007
25
0
0
US
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:
 [Parameter(Mandatory=$false,Position=3)]
 [String]
 $FOO


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




 
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.
 
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.
 
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.
 
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.
 
...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.
 
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
 
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.
 
Let me get rid of mandatory =false.
Thanks for the results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top