?
Solved

PowerShell Local Admin

Posted on 2014-10-29
2
Medium Priority
?
154 Views
Last Modified: 2014-10-29
Is there a way in the following section of script to add a confirmation to the users input and, if they enter no, allow them to re-enter their input? This is part of an AD join script and I am trying to avoid having admins re-run the entire thing if they mistype the user ID.

#Add local user account to the administrators group
$group = "Administrators"
$Host.UI.RawUI.ForegroundColor = "Blue"
$user = Read-Host "Enter the MSAD user ID you wish to grant administrator rights on this PC"
$pc = hostname
$objUser = [ADSI]("WinNT://msad/$user")
$objGroup = [ADSI]("WinNT://$pc/$group")
$objGroup.PSBase.Invoke("Add",$objUser.PSBase.Path)
0
Comment
Question by:myhelpermonkey
[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 29

Expert Comment

by:becraig
ID: 40411606
Just add an additional read-host and use a switch

#Add local user account to the administrators group
$group = "Administrators"
$Host.UI.RawUI.ForegroundColor = "Blue"
$user = Read-Host "Enter the MSAD user ID you wish to grant administrator rights on this PC"
$Conf = Read-Host "You Enter $user is this value correct ? 'Y' yes 'N' no"
switch ($Conf)
{
	"Y"	{
		$pc = hostname
		$objUser = [ADSI]("WinNT://msad/$user")
		$objGroup = [ADSI]("WinNT://$pc/$group")
		$objGroup.PSBase.Invoke("Add", $objUser.PSBase.Path)
	}
"N"	{"You selected NO... Exiting !"; exit }
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
footech earned 2000 total points
ID: 40411620
How about something like the following?
do {
    $user = Read-Host "Enter the MSAD user ID you wish to grant administrator rights on this PC"
    $query = Read-Host "Entered user is: ""$user""`nIs this correct (yes/no)?"
} while ($query -eq "no")

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

777 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