Powershell Active Directory用户名
对于一个学校项目,我需要制作一个Powershell脚本,但是要创建一个用户名,只有人名的第一个字母和完整的第二个名字,有人能帮我吗?这就是我目前拥有的:Powershell Active Directory用户名,powershell,Powershell,对于一个学校项目,我需要制作一个Powershell脚本,但是要创建一个用户名,只有人名的第一个字母和完整的第二个名字,有人能帮我吗?这就是我目前拥有的: Import-Module ActiveDirectory # password for accounts $securePassword = ConvertTo-SecureString "Welkom#1" -AsPlainText -Force # Import the file into a variable
Import-Module ActiveDirectory
# password for accounts
$securePassword = ConvertTo-SecureString "Welkom#1" -AsPlainText -Force
# Import the file into a variable
$users = Import-Csv -Path .\New-GaastraUserBulk.csv
# Loop trough each row, and gather Information
ForEach ($user in $users) {
# Gather the user Information
$fname = $user.FirstName
$lname = $user.LastName
$jtitle = $user.JobTitle
$OUpath = $user.OU
Write-Host $fname
Write-Host $lname
Write-Host $jtitle
Write-Host $OUpath
#Gebruiker aanmaken in AD
New-ADUser -Name "$fname $lname" -GivenName $fname -SamAccountName $lname -Surname $lname -UserPrincipalName "$lname" -Path $OUpath -AccountPassword $securePassword -PasswordNeverExpires $true -Enabled $true
}
根据其他人的评论。在
$lname=…
$sam = "{0}$lname" -f $fname.Substring(0,1)
然后编辑您的新ADUser
行使用$sam
New-ADUser .... -SamAccountName $sam ...
根据其他人的评论。在
$lname=…
$sam = "{0}$lname" -f $fname.Substring(0,1)
然后编辑您的新ADUser
行使用$sam
New-ADUser .... -SamAccountName $sam ...
把我的评论变成答案 您可以很容易地创建用户的SamAccountName,将用户GivenName的第一个字符与完整的LastName结合起来。但是,您需要检查此SamAccountName是否尚未使用 另一件事是,
UserPrincipalName
应该采用@
的形式
要同时使用Splatting改进代码,请执行以下操作:
Import-Module ActiveDirectory
# password for accounts
$securePassword = ConvertTo-SecureString "Welkom#1" -AsPlainText -Force
# Import the file into a variable
$users = Import-Csv -Path .\New-GaastraUserBulk.csv
# Loop trough each row, and gather Information
foreach ($user in $users) {
# first create the desired SamAccountName for the new user
$accountName = "{0}{1}" -f $user.FirstName.Substring(0,1),$user.LastName
# test if a user with that SamAccountName already exists
$checkUser = Get-ADUser -Filter "SamAccountName -eq '$accountName'" -ErrorAction SilentlyContinue
if ($checkUser) {
Write-Warning "SamAccountName $accountName already used for user $($checkUser.Name)"
}
else {
# create a hashtable with all parameters for the New-ADUser cmdlet
$userParams = @{
Name = "$fname $lname"
GivenName = $user.FirstName
Surname = $user.LastName
Title = $user.JobTitle
SamAccountName = $accountName
Path = $user.OU
AccountPassword = $securePassword
PasswordNeverExpires = $true
Enabled = $true
UserPrincipalName = "$accountName@yourdomain.com" # <-- put YOUR domain here after the '@'
# other parameters go here if needed
}
New-ADUser @userParams
}
}
把我的评论变成答案 您可以很容易地创建用户的SamAccountName,将用户GivenName的第一个字符与完整的LastName结合起来。但是,您需要检查此SamAccountName是否尚未使用 另一件事是,
UserPrincipalName
应该采用@
的形式
要同时使用Splatting改进代码,请执行以下操作:
Import-Module ActiveDirectory
# password for accounts
$securePassword = ConvertTo-SecureString "Welkom#1" -AsPlainText -Force
# Import the file into a variable
$users = Import-Csv -Path .\New-GaastraUserBulk.csv
# Loop trough each row, and gather Information
foreach ($user in $users) {
# first create the desired SamAccountName for the new user
$accountName = "{0}{1}" -f $user.FirstName.Substring(0,1),$user.LastName
# test if a user with that SamAccountName already exists
$checkUser = Get-ADUser -Filter "SamAccountName -eq '$accountName'" -ErrorAction SilentlyContinue
if ($checkUser) {
Write-Warning "SamAccountName $accountName already used for user $($checkUser.Name)"
}
else {
# create a hashtable with all parameters for the New-ADUser cmdlet
$userParams = @{
Name = "$fname $lname"
GivenName = $user.FirstName
Surname = $user.LastName
Title = $user.JobTitle
SamAccountName = $accountName
Path = $user.OU
AccountPassword = $securePassword
PasswordNeverExpires = $true
Enabled = $true
UserPrincipalName = "$accountName@yourdomain.com" # <-- put YOUR domain here after the '@'
# other parameters go here if needed
}
New-ADUser @userParams
}
}
您是否正在寻找类似于
“{0}$lname”-f$fname.Substring(0,1)
的内容以用作SamAccountName?。请同时查看New ADUser
cmdlet的多个参数。这将使您的代码更易于维护和阅读你好,我对powershell有点陌生,但我得到了以下信息:new ADUser-Name“$fname$lname”-GivenName$fname-SamAccountName[string]$Initial=$fname.Substring(0,1)$lname应该工作吗?提示:为了获得额外的积分,还要检查是否已经没有这样的samaccountame。例如,Daan Janssen
和Dirk Janssen
都是djanssen
。这是一个常见的问题,在任何大于几个用户的组织中。因此,如果一个用户的名字是Marijn Philippo,我希望SamaAccountName是Mphilippo。如果有人可以编辑我的脚本以这样工作,这将有很大帮助:D因为这是我的第一个脚本!您是否正在寻找类似于“{0}$lname”-f$fname.Substring(0,1)
的内容以用作SamAccountName?。请同时查看New ADUser
cmdlet的多个参数。这将使您的代码更易于维护和阅读你好,我对powershell有点陌生,但我得到了以下信息:new ADUser-Name“$fname$lname”-GivenName$fname-SamAccountName[string]$Initial=$fname.Substring(0,1)$lname应该工作吗?提示:为了获得额外的积分,还要检查是否已经没有这样的samaccountame。例如,Daan Janssen
和Dirk Janssen
都是djanssen
。这是一个常见的问题,在任何大于几个用户的组织中。因此,如果一个用户的名字是Marijn Philippo,我希望SamaAccountName是Mphilippo。如果有人可以编辑我的脚本以这样工作,这将有很大帮助:D因为这是我的第一个脚本!我现在明白了:我也犯了一个错误,但我是全新的。。。那么我需要在哪里添加呢?我已经用确切的位置更新了我的答案你是说这样吗?/对于当前的脚本,这是我的用户输出:不,$sam
是一个全新的行,在你看到的之后,但是现在我得到了这个:它显示了完整的名字,但我只想要它的第一个字母,然后是完整的姓氏!这是我的csv文件:完整当前脚本:我当前得到这个:我也得到了一个错误,但我是全新的。。。那么我需要在哪里添加呢?我已经用确切的位置更新了我的答案你是说这样吗?/对于当前的脚本,这是我的用户输出:不,$sam
是一个全新的行,在你看到的之后,但是现在我得到了这个:它显示了完整的名字,但我只想要它的第一个字母,然后是完整的姓氏!这是我的csv文件:完整的当前脚本:当我运行它时,我得到了:@PrayForMarinusX啊,输入错误。。我已经修正了这个错误:@PrayForMarinusX如前所述。。现在已经解决了。它只需要最后的}
我第一次忘了抄写帖子。为了清楚起见,我还将用户的变量名更改为$checkUser
,当我运行它时,我得到了这样的结果:@PrayForMarinusX啊,typo。。我已经修正了这个错误:@PrayForMarinusX如前所述。。现在已经解决了。它只需要最后的}
我第一次忘了抄写帖子。为了清楚起见,我还将用户的变量名更改为$checkUser