Solved

PowerShell Local Admin

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

This article will help you understand what HashTables are and how to use them in PowerShell.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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