使用 Powershell 脚本设置用户登录名(Windows 2000 之前的版本)

问题描述 投票:0回答:1

How do set this variable using powershell?

$firstName = Read-Host -Prompt "Enter first name: "
$lastName = Read-Host -Prompt "Enter last name: "
$username = Read-Host -Prompt "Enter username: "
...


New-ADUser `
-Name $displayName `
-GivenName $firstName `
-Surname $lastName `
-DisplayName $displayName `
-UserPrincipalName $pName `
-AccountPassword(ConvertTo-SecureString $password -AsPlainText -Force) `
-PasswordNeverExpires $True `
-Description $description `
-EmailAddress $email `
-Path $path `
-Enabled $true
 
 Get-ADUser -Identity $displayName

我正在尝试编写一个用于创建用户的脚本。我想在运行脚本并输入用户信息时设置用户登录名(Windows 2000 之前的版本)。

windows powershell scripting active-directory admin
1个回答
0
投票

您只需要包含

-sAMAccountName
属性。所以

New-ADUser `
-Name $displayName `
-GivenName $firstName `
-Surname $lastName `
-DisplayName $displayName `
-sAMAccountName $samaccountname `
-UserPrincipalName $pName `
-AccountPassword(ConvertTo-SecureString $password -AsPlainText -Force) `
-PasswordNeverExpires $True `
-Description $description `
-EmailAddress $email `
-Path $path `
-Enabled $true
 
 Get-ADUser -Identity $displayName

请注意,请记住 SamAccountName 又名“用户登录名(Windows 2000 之前)”的最大字符长度为 20 个字符。因此,在验证您的输入时,您可能需要检查输入的值是否太长,例如

if ($samaccountname.length -gt 20)
{
   Write-Output "Error, entered username is longer than 20 characters"
   break
}
© www.soinside.com 2019 - 2024. All rights reserved.