Solved

How to create powershell script I can pass a parameter to

Posted on 2014-04-07
8
42,056 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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