因此,在我的脚本中,我创建了一个新的OU并将联系人导入AD中。OU 创建工作正常,但未导入任何用户,并且没有任何错误表明它失败。
New-ADOrganizationalUnit -Name "finance" -Path "DC=ucertify, DC=com"
Import-Module ActiveDirectory
$Users = Import-Csv C:UsersAdministratorDownloadsfinancePersonnel.csv
foreach ($user in $users)
{
$samAccountname = $user.SamAccount
$FirstName = $User.First_Name
$LastName = $User.Last_Name
$PostalCode = $User.PostalCode
$MobilePhone = $User.MobilePhone
$OfficePhone = $User.OfficePhone
$City = $User.City
$Country = $User.Country
}
没有
理由为 CSV 文件中的所有属性创建变量。而是直接使用它们来生成一个哈希表,可用于将参数拆分到New-ADUser
cmdlet,如下所示。
Import-Module ActiveDirectory
# the X.500 path of the Organizational Unit like:
$ouPath = "OU=Finance,DC=ucertify,DC=com"
# test if the OU already exists
if (!(Get-ADOrganizationalUnit -Identity $ouPath)) {
New-ADOrganizationalUnit -Name "Finance" -Path $ouPath
}
Import-Csv C:UsersAdministratorDownloadsfinancePersonnel.csv | Foreach-Object {
# create a hashtable with all the properties from the CSV
# format: ParameterName = Value
$userProps = @{
'Name' = ('{0} {1}' -f $_.First_Name, $_.Last_Name).Trim()
'SamAccountName' = $_.SamAccount
'GivenName' = $_.First_Name
'Surname' = $_.Last_Name
'PostalCode' = $_.PostalCode
'MobilePhone' = $_.MobilePhone
'OfficePhone' = $_.OfficePhone
'City' = $_.City
'Country' = $_.Country
'Enabled' = $true
# I'm guessing you want the user created in the new OU
# If you leave this out, the Path parameter defaults to the Users container.
'Path' = $ouPath
}
# now splat these properties to the New-ADUser cmdlet
New-ADUser @userProps
}
查找新 ADUser cmdlet 以查看可以设置的更多属性。