簡體   English   中英

使用 PowerShell 腳本導入 CSV 用戶

[英]Importing CSV Users with a PowerShell Script

我的目標是通過簡單的 PowerShell 腳本將 CSV 文件中的用戶導入 Active Directory。 盡管如此,我還是遇到了如下所示的語法錯誤。

更新:CSV 列格式

name,surname,ou
Steven,Boone,Management
Rodney,Fisher,Sales
Taylor,Bautista,Management
Nathan,Morris,Management

工作和解決:PowerShell代碼

Import-Module ActiveDirectory
$ADDSUsers = Import-Csv C:\0469697M_gxt.csv

foreach ($user in $ADDSUsers) {
     $Name = $user.name + " " + $user.surname
     $OU = $user.ou
     $OUPath = "OU=$($OU),dc=intgxt,dc=allaboutfood,dc=com,dc=mt"

     #Creating New AD Users                   
     New-ADUser -Name $Name -Path $OUPath
}   

錯誤

New-ADUser : The object name has bad syntax
At C:\Script.ps1:9 char:5
+     New-ADUser -Name "$name" -Path "$OU"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=Tyler Blair,Management:String) [New-ADUser], ADException
    + FullyQualifiedErrorId :ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser

不知道我在哪里出錯。 就我而言,這些列被很好地解析了。

IMO,不起作用的是您提供的 -Path 變量。 您需要提供活動目錄中路徑的 DN (distinguishedName)。 最簡單的方法是獲取用戶的 DN 並獲取父容器 DN。

像這樣的東西:

-Path 'OU=New User Accounts,OU=Users,DC=compost,DC=is,DC=smelly,DC=com'

另外,我建議您習慣每次都以相同的方式分隔字符串並堅持下去。 單引號和雙引號的作用不同。 除非有空格,否則您不必從源 .csv 中分隔字段名稱。

如果您使用單獨的容器,只需在它們運行時構造新用戶對象的父路徑。

$OU

一個可行的例子可能是:

開始

    Import-Module ActiveDirectory
    $ADDSUsers = Import-Csv C:\0469697M_gxt.csv
    $Creation_PW = Read-Host -AsSecureString -Prompt "Choose a password"
    foreach ($user in $ADDSUsers) {
    $Container = "OU=$($OU),OU=Users,DC=contoso,DC=com"
    $FirstName = $user.name
    $LastName = $user.surname
    $Account_Name = "$($FirstName) $($LastName)"
    New-ADUser -ChangePasswordAtLogon $true -Enabled $true -Path $Container -GivenName $FirstName -Surname $LastName -Name $Account_Name Description $Description -AccountPassword $Creation_PW }

結尾

($Creation_PW 應該是 SecureString)

我從用於我的組織的批量帳戶創建腳本中提取了部分。 我們有時會這樣做。 您可以在創建時指定各種屬性,具體取決於您的 AD 架構。 我只留下了相關的部分。

希望這會有所幫助

謝謝大家,我設法完善了我的腳本並讓它全部工作。

我是如何解決這個問題的

如上所述,我不得不為腳本使用 X.500 路徑格式來查找輸入用戶的位置。 對於和我有同樣問題的人,這里是對我有幫助的鏈接:https ://serverfault.com/questions/581383/new-aduser-path-syntax

我還必須使用 -Name 而不是 -GivenName ,然后將 $user.name 和 $user.surname 附加在一起,以便 OU 中的名稱顯示為例如 Dave Smith。 這取決於您的要求。

此外,必須聲明 $($OU),以便腳本知道每個相應用戶的 OU。 (在 .csv 中提供)

如何輸入正確的路徑(X.500 路徑格式)

對於遇到此問題並偶然發現此帖子的任何人。

OU > Domain Name

域名中的點由 dc= 分隔 例如:

Admin Organizational Unit in Contoso.com Domain
$OUPath = "OU=Admin,dc=Contoso,dc=com"

HR Organizational Unit in MyOrganization.co.uk Domain
$OUPath = "OU=HR,dc=MyOrganization,dc=co,dc=uk"

Import Organizational Unit in .CSV File
$OUImport = $user.ou (or whatever your ou column name is)
$OUPath = "OU=($OUImport),dc=MyOrganization,dc=co,dc=uk"

編碼

Import-module activedirectory
$ADDSUsers = Import-csv C:\0469697M_gxt.csv

write-host "Start Process"
write-host "-------------------------------------"

ForEach ($user in $ADDSUsers){
    $Name = $user.name + " " + $user.surname
    $OU = $user.ou
    $OUPath = "OU=$($OU),dc=intgxt,dc=allaboutfood,dc=com,dc=mt"

    #Creating New AD Users                   
    New-ADUser -Name $Name -Path $OUPath
}

特別感謝 (Dave) https://stackoverflow.com/users/9712731/dave和 (EBGreen) https://stackoverflow.com/users/1358/ebgreen的指導。

導入模塊 ActiveDirectory

將 NewUserssent.csv 中的數據存儲在 $ADUsers 變量中

$ADUsers = Import-Csv C:\temp\newuserssent.csv -Delimiter ";"

定義 UPN

$UPN = "波士頓IT.int"

循環遍歷 CSV 文件中包含用戶詳細信息的每一行

foreach ($ADUsers 中的 $User) {

#Read user data from each field in each row and assign the data to a variable as below
$username = $User.username
$password = $User.password
$firstname = $User.firstname
$lastname = $User.lastname
$initials = $User.initials
$OU = $User.ou #This field refers to the OU the user account is to be created in
$email = $User.email
$streetaddress = $User.streetaddress
$city = $User.city
$zipcode = $User.zipcode
$state = $User.state
$country = $User.country
$telephone = $User.telephone
$jobtitle = $User.jobtitle
$company = $User.company
$department = $User.department
$description= $user.description 

# Check to see if the user already exists in 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 {

    # User does not exist then proceed to create the new user account
    # Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser `
        -SamAccountName $username `
        -Description $description `
        -UserPrincipalName "$username@$UPN" `
        -Name "$firstname $lastname" `
        -GivenName $firstname `
        -Surname $lastname `
        -Initials $initials `
        -Enabled $True `
        -DisplayName "$lastname, $firstname" `
        -Path $OU `
        -City $city `
        -PostalCode $zipcode `
        -Country $country `
        -Company $company `
        -State $state `
        -StreetAddress $streetaddress `
        -OfficePhone $telephone `
        -EmailAddress $email `
        -Title $jobtitle `
        -Department $department `
        -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True

    # If user is created, show message.
    Write-Host "The user account $username is created." -ForegroundColor Green
}

}

讀取主機 - 提示“按 Enter 退出”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM