将多个用户添加到AD,CSV

时间:2018-07-16 15:52:43

标签: powershell powershell-v4.0

我正在尝试通过CSV文件夹导入用户。

我需要保留某些参数,因此我仅使用某些字段。

Powershell

$csv = Import-Csv -Path "newusers.csv"
foreach ($User in $csv)
{
#region Data Generation

$DisplayName = $User.'Surname' + " " + $User.'GivenName'

$Mail = $User.'GivenName' + "." + $User.'Surname' + "@" + "royalberkshire.nhs.uk"

$MailAlias = $User.'GivenName' + "." + $User.'Surname' + "@" + $DNSRoot2

$SInitial = $User.'Surname'[0]
$Initial = $User.'GivenName'[0]
$SAMAccountName = $User.'Surname' + "" + $Initial
$SAMAccountLower = $SAMAccountName.ToLower()
$UserPrincipalName = $User.'Surname'+$Initial
$HD = "U"
$HDir = "\\RBHFILRED002\"
$AC = "Users_01$\"
$DH = "Users_02$\"
$IM = "Users_03$\"
$NS = "Users_04$\"
$TZ = "Users_05$\"

$Folder = if ($SInitial -in 'a','b','c'){$AC}
          ElseIf ($SInitial -in 'd','e','f', 'g','h'){$DH}
          ElseIf ($SInitial -in 'i','j','k', 'l','m'){$IM}
          ElseIf ($SInitial -in 'n','o','p', 'q','r','s'){$NS}
          Else {$TZ}

$group1 = "zz Everyone"
$group2 = "Safeboot Domain Users"

$defaultname = $SAMAccountName
$email = $User.'GivenName' + "." + $User.'Surname'
$i = 1
cls

# Create The User



While ((Get-ADUser -Filter "SamAccountName -eq '$SAMAccountName'" -ErrorAction SilentlyContinue) -ne $null){
    $SamAccountName = $defaultname + [string]$i 
    $Mail = $email + [string]$i + "@" + "royalberkshire.nhs.uk"
    $i++

}


$NewUserParams = @{
    path                  = "OU=Users,OU=RBFT,DC=rbbh-tr,DC=nhs,DC=uk"
    SamAccountName        = $SAMAccountName
    Name                  = $SAMAccountName
    DisplayName           = $DisplayName
    GivenName             = $User.'GivenName'
    Surname               = $User.'Surname'
    EmailAddress          = $Mail
    UserPrincipalName     = "$SAMAccountName@rbbh-tr.nhs.uk"
    Title                 = $title
    HomeDrive             = $HomeDrive
    HomeDirectory         = "$HDir$Folder$SAMAccountName"
    Description           = $User.'Description'
    ChangePasswordAtLogon = $true
    PasswordNeverExpires  = $false
    AccountPassword       = $password
    Enabled               = $true
}



New-ADUser @NewUserParams -ErrorAction SilentlyContinue
Add-ADGroupMember -Identity $group1  -Members $SAMAccountName
Start-Sleep -s 10
Add-ADGroupMember -Identity $group2  -Members $SAMAccountName


cls
echo "Please Wait Whilst We Create The AD Account & Create The Exchange Mailbox.."
Start-Sleep -s 30

Enable-Mailbox -Identity $SAMAccountName
cls

echo "Please Wait Whilst We Activate The Exchange Mailbox..."
Start-Sleep -s 15

# Sets The User Up With The Randomised Password, And Re-Encrypts It For Double Protection
Set-ADAccountPassword -Identity $SAMAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $random -Force)
cls

}

CSV

User    GivenName   Surname Description
User    James        Timms     Test
User    James        Timms     Test
User    Hulk         Hogan     Test
User    Ultimate     Warrior   Test
User    The          Rock      Test
User    Dwayne       Johnson   Test

该脚本无法运行。它告诉我无法识别搜索过滤器。

这只是我的错误。

使用Write-Hosts和Inputs,可以与单个用户配合使用。

但是对于CSV来说,它不起作用。

我必须注意,这也是我第一次在Powershell上通过CSV创建用户。

有人对如何解决此问题有任何想法吗?

1 个答案:

答案 0 :(得分:0)

我知道了,

结果是,当我在Excel 2016中构建CSV时,没有将逗号添加到单独的值中。

我最终在记事本中打开CSV并添加了逗号来分隔值。

Powershell会基于逗号分隔读取值,因此,如果没有逗号,它将不知道要推出哪些值。