Solved

Powershell script with user input

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
In-place Upgrading Dirsync to Azure AD Connect
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

738 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