Solved

PowerShell Local Admin

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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