嘿,所以我试图在 python 中从 powershell 制作相同的脚本,但我无法正确执行,也许你可以帮助我,所以脚本是这样的:
打开包含用户信息的 csv 文件
为 csv 文件中的每一行创建用户
csv 看起来像这样:
powershell 脚本工作正常,如下所示:
# import module
Import-Module ActiveDirectory
# create new password
$securedpassword = ConvertTo-SecureString "abc-123" -AsPlainText -Force
#import csv
$filepath = Read-Host -Prompt "please enter csv path"
#import the file into a variable
$users = Import-Csv $filepath
# loop all rows to gather information
foreach ($user in $users) {
# gather user information
$fname = $user.'first name'
$lname = $user.'last name'
$oupath = $user.'ou'
#creat new ad user from csv file
New-ADUser -name "$fname $lname" -GivenName $fname -Surname $lname -UserPrincipalName "$fname.$lname" -Path $oupath -AccountPassword $securedpassword -ChangePasswordAtLogon $true -Enabled $true
# echo output
echo "account created for $fname $lname in $oupath"
}
在Python中是这样的:
#import csv and active directory module
import csv
from pyad import *
def createuserfromcsv():
#takes full file path for test: c:\newusers.csv
file = input('please type your file path + file: ')
data = open(file,encoding="utf-8")
csv_data = csv.reader(data)
data_lines = list(csv_data)
pyad.set_defaults(ldap_server="DC-01-Training.Udemy.training",username="Administrator",password="abc-123")
for line in data_lines[1:]:
user = line[0]
for line in data_lines[1:]:
oupath = line[2]
ou = pyad.adcontainer.ADContainer.from_dn(oupath)
new_user = pyad.aduser.ADUser.create(user,ou,password="abc-123")
print(user)
print(oupath)
我怎样才能解决这个问题 ?
忽然笑
aluckdog
SMILET
相关分类