3 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
好吧,我成功了,無論有沒有功能都可以:
import csv
from pyad import *
def createuserfromcsv():
#takes full file path
file = input('please type your file path + file: ')
data = open(file,encoding="utf-8")
csv_data = csv.reader(data)
data_lines = list(csv_data)
#load admin information
pyad.set_defaults(ldap_server="DC-01-Training.Udemy.training",username="Administrator",password="abc-123")
for line in data_lines[1:]:
user = line[0]
oupath = line[2]
ou = pyad.adcontainer.ADContainer.from_dn(oupath)
pyad.aduser.ADUser.create(user,ou,password="abc-123")

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊
# Step 1: Install the required python modules
!pip install pyad
!pip install pandas
# Step 2: Import the necessary modules in your script
import pyad
import pandas as pd
# Step 3: Connect to the AD server
ad_user = "user@example.com"
ad_password = "password"
ad_domain = "example.com"
pyad.set_defaults(username=ad_user, password=ad_password)
ad_server = pyad.adserver.ADServer.from_domain_name(ad_domain)
# Step 4: Read the CSV file into a Pandas DataFrame
df = pd.read_csv("records.csv")
# Step 5: Iterate over the rows of the DataFrame and create AD users
ou = "OU=Users,DC=example,DC=com" # The distinguished name of the AD organizational unit where you want to create the users
for index, row in df.iterrows():
first_name = row["first_name"]
last_name = row["last_name"]
email = row["email"]
password = row["password"]
# Create the AD user
user = pyad.aduser.ADUser.create(name=f"{first_name} {last_name}", upn=email, password=password, ou=ou)
# Set additional attributes for the user
user.update_attribute("givenName", first_name)
user.update_attribute("sn", last_name)
user.update_attribute("mail", email)
# Step 6: Save the changes to the AD server
ad_server.commit()
print("Import completed successfully!")
該腳本執(zhí)行以下步驟:
使用 pip 命令安裝所需的 python 模塊 pyad 和 pandas。
導(dǎo)入 pyad 和 pandas 模塊。
使用提供的域名和憑據(jù)連接到 AD 服務(wù)器。pyad.set_defaults() 函數(shù)用于設(shè)置連接的默認(rèn)用戶名和密碼。pyad.adserver.ADServer.from_domain_name() 函數(shù)用于根據(jù)提供的域名創(chuàng)建 AD 服務(wù)器對(duì)象。
使用 pandas.read_csv() 函數(shù)將 CSV 文件讀入 Pandas DataFrame。
使用 df.iterrows() 函數(shù)迭代 DataFrame 的行。對(duì)于每一行,腳本提取相關(guān)列的值(例如名字、姓氏、電子郵件、密碼)并將它們存儲(chǔ)在變量中。
使用 pyad.aduser.ADUser.create() 方法創(chuàng)建新的 AD 用戶。name 參數(shù)指定用戶的全名,upn 參數(shù)指定用戶主體名稱(電子郵件地址),password 參數(shù)指定用戶的密碼。ou 參數(shù)指定要在其中創(chuàng)建用戶的 AD 組織單位的可分辨名稱。
使用 user.update_attribute() 為用戶設(shè)置附加屬性

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
# Step 1: Import the ActiveDirectory module and connect to the AD server
Import-Module ActiveDirectory
$ad_user = "user@example.com"
$ad_password = "password" | ConvertTo-SecureString -AsPlainText -Force
$ad_credential = New-Object System.Management.Automation.PSCredential -ArgumentList $ad_user, $ad_password
$ad_domain = "example.com"
Connect-AzureAD -Credential $ad_credential -TenantId $ad_domain
# Step 2: Read the CSV file into a variable as an array of objects
$records = Import-Csv -Path "records.csv"
# Step 3: Iterate over the array of objects and create AD users
foreach ($record in $records) {
$first_name = $record.first_name
$last_name = $record.last_name
$email = $record.email
$password = $record.password
# Create the AD user
New-AzureADUser -AccountEnabled $true -DisplayName "$first_name $last_name" -PasswordProfile @{ Password = $password; ForceChangePasswordNextSignIn = $false } -UserPrincipalName $email -GivenName $first_name -Surname $last_name -MailNickname $email
# Set additional attributes for the user
Set-AzureADUser -ObjectId (Get-AzureADUser -Filter "UserPrincipalName eq '$email'").ObjectId -MobilePhone $record.mobile_phone -StreetAddress $record.street_address -City $record.city -State $record.state -PostalCode $record.postal_code -Country $record.country
}
# Step 4: Save the changes to the AD server
Save-AzureAD
Write-Host "Import completed successfully!"
將腳本保存到擴(kuò)展名為 .ps1 的文件中,例如 Import-ADUsers.ps1。
修改腳本為以下變量設(shè)置適當(dāng)?shù)闹担?/p>
$ou:這是您要在其中創(chuàng)建新用戶的組織單位 (OU) 的可分辨名稱 (DN)。
$csvFile:這是包含要導(dǎo)入的用戶記錄的 CSV 文件的路徑。
確保 CSV 文件具有腳本中指定的適當(dāng)列。
打開 PowerShell 窗口并導(dǎo)航到保存腳本的目錄。
使用以下命令運(yùn)行腳本: .\Import-ADUsers.ps1 該腳本將讀取 CSV 文件,為文件中的每條記錄創(chuàng)建一個(gè)新的用戶對(duì)象,并為每個(gè)用戶設(shè)置指定的屬性。
請(qǐng)注意,您需要在計(jì)算機(jī)上安裝 Active Directory 模塊,并且需要使用管理權(quán)限運(yùn)行腳本。
添加回答
舉報(bào)