【发布时间】:2020-10-27 02:16:01
【问题描述】:
我正在编写通过 PowerShell 使用用户(创建者)输入创建新用户的脚本。我正在寻找的输入是名字和姓氏以及一些属性。我希望从输入中自动创建 samaccountname 和 UPN。不确定这是否可以完全完成,但想对我当前的脚本进行一些输入。我突出显示了 firstinitial 作为占位符,以显示我想要完成的任务。
new-aduser -givenname($givenname = read-host "Input Firstname") -surname($surname = read-host "Input Lastname") -samAccountName ("***firstinitial***"+"$._surname") -userprincipalname "$._surname+"@domain.com" -path "OUName" -whatif
好的,感谢以下帮助。我能够进行更多搜索,并且可以进行以下搜索。除了显着的名称以单个名称出现而不是名字和姓氏之间的空格之外,所有看起来都可以工作。
#User info entered
$first = Read-Host "First name"
$last = Read-Host "Last name"
$title = Read-Host "Title"
$location = Read-Host "Location"
$department = Read-Host "Business Practice"
$password = read-host -assecurestring "Password"
#Create new user
$Attributes = @{
Enabled = $true
ChangePasswordAtLogon = $false
UserPrincipalName = $first.split(" ")[0]+$last+"@domain.com"
Name = $first+$last
GivenName = $first
Surname = $last
DisplayName = "$first "+" $last"
Office = $location
Department = $department
Title = $title
samAccountName = $first.split(" ")[0] + $last
AccountPassword = $password
}
New-ADUser @Attributes -whatif
【问题讨论】:
-
澄清一下,如果我使用 Jane Doe 的名字,我会得到以下信息 - (注意 JaneDoe 中没有空格)“CN=janedoe,CN=Users,DC=KFriese,DC=office”
-
好的,解决了这个名字组合的问题。
-
$first + " " + $last或"{0} {1}" -f $first, $last
标签: powershell