让 powershell 脚本提示输入其运行时的用户名和密码

让 powershell 脚本提示输入其运行时的用户名和密码

这是我的基本用户帐户创建脚本。我想使用 Runas 让它以域帐户的形式运行 New-Aduser。理想情况下,我会为该帐户添加输入提示。有什么建议吗?我无法让它工作。当我使用管理员帐户远程访问另一台计算机并运行该脚本时,该脚本确实工作正常。

#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

答案1

你调查过吗Get-Credential

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

这将提示输入用户名和密码。然后您可以$domaincred使用New-ADUser

New-ADUser @Parameters -credential $domaincred

相关内容