Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell script with user input

Posted on 2012-03-20
2
Medium Priority
?
491 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:
Brent Challis earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

824 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