Solved

How to create powershell script I can pass a parameter to

Posted on 2014-04-07
8
38,776 Views
Last Modified: 2014-04-08
I would like to write a script such that I can pass a variable by the command line to the script. Example would be like below.

.\script.ps1 computername

Open in new window


Usually in my scripts I would actually enter my variables within the script itself.  A very simple example would be

get-adcomputer computername

Open in new window


What would I need do to to allow me to pass a parameter to the script through the command line and have it return a value?
0
Comment
Question by:Joseph Daly
  • 4
  • 3
8 Comments
 
LVL 14

Assisted Solution

by:Justin Yeung
Justin Yeung earned 100 total points
ID: 39984161
change your script as a function.

#>
function Verb-Noun #example get-something
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string]$computername

       
    )

{your script}

}
0
 
LVL 35

Author Comment

by:Joseph Daly
ID: 39984188
Im sure that would work but would you mind explaining what exactly that does and how it does it. Looking at the code I am pretty over my head.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39984207
so what happen is, instead of set the variable within the script, you can specific it as a "required parameter" with (Mandatory=$true).

when you run the script, it will come up and ask you what is $computername will be by typing it in.

powershell can convert script that run to a function or module that can be used with different variable setup.

here is the full powershell 3.0 tutorial from Microsoft Academy, which explain and you can learn from it step by step to powershell.

http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39984213
This course actually explain it really well, and I am sure you can learn from it.
0
 
LVL 35

Author Comment

by:Joseph Daly
ID: 39984268
How would I create a function so that I could pass a .\name.ps1 parameter?
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39984399
sorry I don't understand your question.....
0
 
LVL 17

Accepted Solution

by:
Learnctx earned 400 total points
ID: 39984783
Don't worry about cmdlet binding and other advanced functions. Just stick to basic named parameters until you get your head around it all. If you want to pass a parameter to powershell you can do it named, or unnamed. An unnamed parameter is position (taken in the order it is given).

## Top of the script
$ComputerName = $args[0]
$ComputerName

Open in new window

This would say take:

.\script.ps1 computer1

$ComputerName would be assigned the value computer1. If you were to do:

.\script.ps1 computer1 computer2

You would need to assign both values with $args[0] and $args[1]. $args is an array of all the arguments you provide to a script or function. You can see this is not ideal. It relies on the arguments in the right position always. This is where a named parameter comes in.

## Top of the script
param (
   [string]$ComputerName
)
$ComputerName

Open in new window

This will let you launch your script with a named parameter like so:

.\script.ps1 -ComputerName Computer1

Casting as a string "[string]" is not required, but its good practice.

If you want to get a bit more advanced, cast it as a string with accepts multiple values.

## Top of the script
param (
   [string[]]$ComputerName
)
Foreach ($Computer in $ComputerName)
{
   $Computer
}

Open in new window

This will let you launch your script with multiple computer names specified:

.\script.ps1 -ComputerName Computer1, Computer2, Computer3, Computer4

If you want to know more about what Justin Yeung is talking about after understanding parameters better look into "Powershell Advanced Functions" on Google. There are a number of good articles. Few Powershell userse will have a need to used Advanced Functions. I recommend getting a better understanding of named and unnamed parameters for scripts and functions first.
0
 
LVL 35

Author Closing Comment

by:Joseph Daly
ID: 39986957
Thank you the explanation really helped.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question