【发布时间】:2020-09-03 07:29:07
【问题描述】:
嘿,所以我正在尝试从 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)
我该如何解决这个问题?
【问题讨论】:
-
为什么你的csv中的每一行都用方括号括起来???
-
不是电子表格,你看到它的输出是:print(data_lines)
-
所以您向我们展示的根本不是 csv 文件。请编辑您的问题,并输入有效的 csv 数据。
-
好的,现在我添加了原始 csv 文件的图片
标签: python powershell csv active-directory