Kylo Ren
asked on
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
ex.
ping.ps1 mycomputer
ASKER
thanks, i found that link before but it is a little confusing. was hoping someone has a easier example
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.
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.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Usage:
.\ScriptName "127.0.0.1"
HTH,
Dale Harris
.\ScriptName "127.0.0.1"
HTH,
Dale Harris
ASKER
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
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
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
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
This is a more extensive example, the param block is exceptionally powerful.
Chris
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
Fun, huh?Chris
ASKER
Dale, Thanks for the explanation. I am now comfortable with parameters! thanks again
http://devcentral.f5.com/weblogs/Joe/archive/2009/01/13/powershell-abcs---p-is-for-parameters.aspx