Parameter syntax question

by Lery at 2013-04-24 14:20:06

If I put this into my script:


param(
[Parameter(Mandatory=$True,
HelpMessage="Computer name or IP address to query")]
[string]$computername
)


It will prompt me for computername[0]. I enter in the name or ip and hit enter. It then prompts me for computername[1]. If I hit enter again it runs my script. Is there a syntax to make it just prompt me once. I enter in the computername and hit enter, and that is it. I’m not interested in entering in multiple computer names.
by mjolinor at 2013-04-24 14:36:02
Change this:

[string]$computername
to this:
[string]$computername

Any time you see those nested square brackets in a type, it’s designating an array of objects of that type. Without them, it means a single object.
by Lery at 2013-04-24 15:53:39
[quote="mjolinor"]Change this:

[string]$computername
to this:
[string]$computername

Any time you see those nested square brackets in a type, it’s designating an array of objects of that type. Without them, it means a single object.[/quote]

Gotcha.

Is it better to do that or this:


#$a = Read-Host "Please Enter Computer Name or IP Address"
#$computername = $a


The code just above seems to work better in the manner of running the script by right clicking and choose run with powershell. As opposed to the parameter code. When I right click on the script when it utilizes a parameter, it just opens and closes. I have to actually launch from within Powershell. Which is OK in some circumstances. I also like the Read-Host because it looks a little nicer than a prompt saying computername:. Am I breaking any rules by using the read-host command?