Solved

Powershell script with user input

Posted on 2012-03-20
2
479 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Suggested Courses

635 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