Solved

Powershell script with user input

Posted on 2012-03-20
2
467 Views
Last Modified: 2012-03-21
I need to create a script that I can run that will prompt for a Group Name and then Run a command to change the permissions of that group name.

So I have:

$name=Read-Host "Group Name"

Which works

and then I have

add-adpermission -Identity "$name" -user "all nei employees" -accessrights writeproperty -properties "member"

They both work separately but how can I combine them into one script?
0
Comment
Question by:npdodge
2 Comments
 
LVL 8

Accepted Solution

by:
bchallis earned 500 total points
ID: 37745298
There are several options.  The most straightforward is to put the two lines in a text file with an extension of .ps1.  You can then just open up the PowerShell console and type the name of the script file and it will execute both lines.  

This assumes that you have configured your environment to run scripts as by default the running of scripts is disabled.  If you need to enable scripts you can do this with a GPO or by running the PowerShell console as Administrator and running the command:

Set-ExecutionPolicy RemoteSigned.

There are other policy options, I feel that this is the best as local scripts will run without having a digital signature but scripts run from network locations must be digitally signed before they will run.

The second step in the script file would be to use the name as a parameter.  To do this you would put:
Param
(
    $name=(Read-Host "Group Name")
)

at the beginning of the file.  That way you can run the script as .\ScriptName.ps1 nameToUse

and nameToUse would automatically be assigned to $name.  With the way I have written the parameter declaration, if you do not enter a name on the command line you will be prompted for one.

As a note, when you call a script file to run you must either provide a path to the file (in the case above I used .\ to indicate it was in the current directory) or the file must be in a directory in your Path environment variable.

The third option in the case that this is something that you do frequently would be to make it a function in your profile script so you could just open the PowerShell console and call the function.  If you think that you would like to move in this direction let me know and I will a description of how to do it.
0
 

Author Closing Comment

by:npdodge
ID: 37747285
I used the param method and it worked well
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

707 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

17 Experts available now in Live!

Get 1:1 Help Now