Solved

Powershell script with user input

Posted on 2012-03-20
2
469 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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

832 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