Powershell parameter

how do i use the parameter function? lets say for example i want to create a function that will ping a host. i then want to use the parameter function to specify a hostname.

ex.
ping.ps1 mycomputer
LVL 5
Kylo RenSystem EngineerAsked:
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.

jjardineCommented:
you might try using the $args[0] variable.   It might be 1 instead of 0.  Here is some more info:

http://devcentral.f5.com/weblogs/Joe/archive/2009/01/13/powershell-abcs---p-is-for-parameters.aspx
0
Kylo RenSystem EngineerAuthor Commented:
thanks, i found that link before but it is a little confusing. was hoping someone has a easier example
0
jjardineCommented:
Do you have any more of your code available to show how you are using the variable. you should be able to just add the  arg to the ping request..


ping code + $args[0]

I think 0 is the first parameter.  In some languages it is the name of the script, so it might be 1 instead.
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Dale HarrisProfessional Services EngineerCommented:
Try this easy script:

param (
[string]$HostIP
)
if ($HostIP -ne "")
{
"Host IP = $HostIP"
}
else
{
$HostIP = Read-Host "Please enter in the IP of the host you want to ping"
}

Ping $HostIP
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
Dale HarrisProfessional Services EngineerCommented:
Usage:

.\ScriptName "127.0.0.1"

HTH,

Dale Harris
0
Kylo RenSystem EngineerAuthor Commented:
dale thats exactly what i was looking for.

is this the line that creates the pop up?

param (
[string]$HostIP
)

Also, what does this line do?

{
"Host IP = $HostIP"
}

do you need the last line since i will be passing the host ip through the script? ex. .\script ip
Ping $HostIP

sorry about the extra questions. i want to make sure i understand it though

0
Dale HarrisProfessional Services EngineerCommented:
The script works like the following:

param (
#Whatever you put in here will be your parameter information, no prompts for information
)

So if the value of $HostIP actually is something and not "" or blank, it will show you what it is:
"Host IP = $HostIP" and it will show up as "Host IP = <whatever you put as the parameter>"

You do need the last line because that's the actual code to ping the host you're putting through the parameter.

It's pretty much saying

Ping google.com

if google.com was your parameter

I enjoy explaining this to you and I think Powershell is very much worth your time to learn.

DH
0
Chris DentPowerShell DeveloperCommented:
This is a more extensive example, the param block is exceptionally powerful.
Function Start-Ping {
  # Optional: Extended behaviour can be enabled in PowerShell 2 with CmdLetBinding
  # [CmdLetBinding( <Parameters> )]
  # e.g. [CmdLetBinding( SupportsShouldProcess = $True )]
  # Start of parameter definition
  Param(

    # Parameter properties, such as mandatory and position and so on
    [Parameter( Mandatory = $True, ValueFromPipeline = $True )]
    # Parameters can be validated using a number of different methods. For example, make sure
    # it has a value
    [ValidateNotNullOrEmpty()]
    # Parameters can be forced to a specific type. For example, String
    # Then a parameter name is defined and a default value can be assigned (optionally)
    [String]$HostName = $Env:ComputerName

    # Repeat for any other parameters (a comma separated list
  )

  Process {
    ping $HostName -n 1
  }
}

# This will happily continue, pinging the local machine ($Env:ComputerName is an 
# environmental variable)
Start-Ping

# This will tell you off for passing a blank value
Start-Ping ""

# These two are basic usage
Start-Ping localhost
Start-Ping -HostName localhost

# This is the pipeline part (ValueFromPipeline = $True)
"localhost" | Start-Ping

# Or a list from the pipeline (permissible because we used Process { })
"localhost", "someremotehost" | Start-Ping

Open in new window

Fun, huh?

Chris
0
Kylo RenSystem EngineerAuthor Commented:
Dale, Thanks for the explanation.  I am now comfortable with parameters! thanks again
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.