Solved

How to create powershell script I can pass a parameter to

Posted on 2014-04-07
8
34,233 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 16

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now