Having a powershell script prompt for the username and password it runs under

0

1

Here is my basic user account creation script. I would like to use Runas to have it run New-Aduser as a domain account. Ideally I would add an input prompt for the account. Any advice? I haven't been able to get it to work. The script does work fine when I remote into another machine with an administrative account and run it.

#credentials
?


#Input Prompts
$Firstname   = Read-Host 'What is users first name?'
$Lastname    = Read-Host 'What is users last name?'
$UserID      = Read-Host 'What is users ID?'



#Static Variables
$Password     = (ConvertTo-SecureString -AsPlainText 'PASSWORD' -Force)
$Displayname  = "$lastname, $firstname BCA:EX"
$OUPath       = 'OU=AAA Users,OU=AAA,OU=AAA,OU=AAA,OU=AAA,OU=AAA,DC=AAA,DC=AAA'


#used to add parameters
$Parameters = @{
    'SamAccountName'        = $UserID
    'UserPrincipalName'     = $UserID 
    'Name'                  = $Firstname
    'GivenName'             = $Firstname
    'Surname'               = $Lastname
    'DisplayName'           = “$Displayname” 
    'AccountPassword'       = $password
    'ChangePasswordAtLogon' = $true 
    'Enabled'               = $true 
    'Path'                  = $OUPath
}
import-module activedirectory
New-ADUser @Parameters

Jim De Goede

Posted 2017-04-19T19:27:04.880

Reputation: 54

Answers

2

Have you investigated Get-Credential?

$domaincred = get-credential -Message "Enter a domain account that can create a new user on the domain"

This will prompt for a username and password. You can then use $domaincred in New-ADUser.

New-ADUser @Parameters -credential $domaincred

Tim Haintz

Posted 2017-04-19T19:27:04.880

Reputation: 321