我在这里写了一个脚本,用于检查 AD 是否已存在。如果它们确实存在,那么脚本会通知我。如果它们不存在,那么它会创建它们:
if (Get-ADUser -F { SamAccountName -eq $username }) {
# If user does exist, give a warning
Write-Warning "A user account with username $username already exists in Active Directory."
else {
##Then it carries on here with creating the user
我想做的是,如果他们存在,则使用相同的命名约定创建用户,但在其名称后添加“1”。我知道这听起来很傻……但我被问到这是否可行。
如果它们不存在,那么就照常进行并创建它们:)
女士们先生们,任何帮助都将不胜感激。
谢谢 :)
答案1
在带有警告的 if 语句中,只需添加
$username = $username + "1"
然后在 else 部分重复该代码。
可以随时清理它并将创建帐户代码放在一个函数中,并在 if 语句的每个部分中调用它。
答案2
##完整脚本##
$ADUsers = Import-Csv C:\ps\usercreation.csv
$UPN = "METest.local"
foreach ($User in $ADUsers) {
#Assign the data in the csv to a variable
$username = $User.username
$password = $User.password
$firstname = $User.firstname
$lastname = $User.lastname
$OU = $User.ou #This field refers to the OU the user account is to be created in
$telephone = $User.telephone
$jobtitle = $User.jobtitle
#Check and see if the user already exists in AD
if (Get-ADUser -F { SamAccountName -eq $username }) {
#Warn if the users already exists
Write-Warning "A user account with username $username already exists in Active Directory."
}
else {
#User does not exist then proceed to create the new user account
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "[email protected]" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-OfficePhone $telephone `
-Title $jobtitle `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -PasswordNeverExpires:$false -ChangePasswordAtLogon:$True
#If this user exists
Write-Host "The user account $username is created." -ForegroundColor Cyan
#Sleep time to sync for Exchange push
#write-host Pausing for Exchange Sync....
#start-sleep -Seconds 300
#Apply licenses to users created in AD then synced to Exchange
#Set-MsolUserLicense -UserPrincipalName $newuser.UserPrincipalName -AddLicenses UI-OF-LICENSE-HERE
#Set-MsolUserLicense -UserPrincipalName $newuser.UserPrincipalName -AddLicenses UI-OF-LICENSE-HERE
}
}
Read-Host -Prompt "Press Enter to exit"
别在意所有的评论……它们在那里,所以在我测试的时候它不会运行某些东西
答案3
因此,首先您不需要将 csv 中的值分配给变量,它们已经加载到对象中并驻留在内存中,因此您可以将 $username 切换为 $user.username,对添加用户部分重复此操作,只需在数据集的句点后更改名称即可。我想我知道发生了什么,所以如果你的系统中有一个 john doe,而你的列表中还有另外 2 个 john doe,第一次创建了一个 john.doe1 并且成功了,但第二次失败了。因此,您需要将用户名字符串修剪到最后一个字符,进行比较以查看最后一个字符是字符还是数字。如果是字母,则连接 1;如果是数字,则将 1 添加到它并连接该数字。明白了吗?在我的手机上,所以编写代码和研究语法很痛苦,抱歉。
答案4
我并没有真正触及注释掉的部分,但希望这能给你一个想法。我没有 AD 服务器来测试它,只是要知道我不能保证它 100%
function validation($User){
if (Get-ADUser -F { SamAccountName -eq $User.username }) {
$usernumber = 1
while (Get-ADUser -F { SamAccountName -eq ($User.username + $usernumber)}){
$usernumber++
}
$NewUserName = $User.username + $usernumber
Add_User($NewUserName, $User)
}
else {
$NewUserName = $User.username
Add_User($NewUserName, $User)
}
}
function Add_User($Username, $User){
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username" + "@MEtest.com" `
-Name "$Firstname $Lastname" `
-GivenName $User.Firstname `
-Surname $User.Lastname `
-Enabled $True `
-DisplayName "$User.Lastname, $User.Firstname" `
-Path $User.ou `
-OfficePhone $User.telephone `
-Title $User.jobtitle `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -PasswordNeverExpires:$false -ChangePasswordAtLogon:$True
}
$ADUsers = Import-Csv C:\ps\usercreation.csv
$UPN = "METest.local"
foreach ($User in $ADUsers) {
validation($User)
#If this user exists
Write-Host "The user account $username is created." -ForegroundColor Cyan
#Sleep time to sync for Exchange push
#write-host Pausing for Exchange Sync....
#start-sleep -Seconds 300
#Apply licenses to users created in AD then synced to Exchange
#Set-MsolUserLicense -UserPrincipalName $newuser.UserPrincipalName -AddLicenses UI-OF-LICENSE-HERE
#Set-MsolUserLicense -UserPrincipalName $newuser.UserPrincipalName -AddLicenses UI-OF-LICENSE-HERE
}
}
Read-Host -Prompt "Press Enter to exit"