Powershell - Pass Args

I have a working Powershell script usersearch.ps1 that searches Active Directory, but it uses read-host to prompt for input.  Iam trying to change the script such that I can pass an argument to my the script or leave the argument blank, and have the script prompt me with the $var=read-host line and continue on with the search.  If I call usersearch -[string], the script happily searches my AD and returns the correct results.  If I leave out the -[string] it blows up with the error below and never runs the else portion of the IF statement which has the $var=read-host  prompt.

Was that clear?    Help...?


Param([string] $u="")
    if ($u -ne $null)
        {
        $searchv=$u
        }
    else            
        {
        $u=read-host "Enter the user's name or logon_id to search for"
        }

$searchv=$u
                  
$exact=(get-QADUser -SearchRoot Domain.com/Users $searchv).DirectoryEntry.samAccountName

.
.
.
.
[PS] C:\>usersearch
Get-QADUser : Invalid identity
At C:\Tools\usersearch.ps1:13 char:20
+ $exact=(get-QADUser <<<<  -SearchRoot Domain.com/Users $searchv).DirectoryEntry.samAccountName
    + CategoryInfo          : NotSpecified: (:) [Get-QADUser], Exception
    + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.GetUserCmdlet
hcadminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
"" is not $null, so your read-host is never executed. Proof:
if ($null -eq "") {echo "is equal"} else {echo "is not equal"}

Open in new window

But the usual way to define mandatory fields or default actions is different and more compact:
Param([string] $u = (read-host "Enter the user's name or logon_id to search for"))

$searchv=$u
$exact=(get-QADUser -SearchRoot Domain.com/Users $searchv).DirectoryEntry.samAccountName

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hcadminAuthor Commented:
Thanks, I like your solution.  I also tried

$u=$args[0]
   
    if ($u -ne $null)
        {
          blah, blah.....

which worked, but now I need to check how many args they try and pass to me and either reject if >1 or accept and run a ForEach $i in $args.......

Thanks again
0
hcadminAuthor Commented:
There are a few ways to do this, but once I add error checking it may lend itself to one solution over the other.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.