Solved

PowerShell Local Admin

Posted on 2014-10-29
2
146 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
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 39

Accepted Solution

by:
footech earned 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now