Solved

How to create powershell script I can pass a parameter to

Posted on 2014-04-07
8
39,994 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

740 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