Joseph Daly
asked on
How to create powershell script I can pass a parameter to
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.
Usually in my scripts I would actually enter my variables within the script itself. A very simple example would be
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?
.\script.ps1 computername
Usually in my scripts I would actually enter my variables within the script itself. A very simple example would be
get-adcomputer computername
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
This course actually explain it really well, and I am sure you can learn from it.
ASKER
How would I create a function so that I could pass a .\name.ps1 parameter?
sorry I don't understand your question.....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you the explanation really helped.
ASKER