【问题标题】:How to add info to the AD users with powershell from a CSV sheet如何使用 CSV 表中的 powershell 向 AD 用户添加信息
【发布时间】:2013-11-05 13:37:42
【问题描述】:

假设我有一个 CSV 表,第一行是用户名,第二行是电子邮件地址。

例子:

用户名电子邮件地址


jhornet jhornet@mail.com

我怎样才能以一种安全且良好的方式将此信息导入 AD 中,也许需要签入?

这是我到目前为止所拥有的(没有 CSV):

Import-Module activedirectory

$company = "International"
$username = Get-Content c:\users.txt
$emailadress = Get-Content c:\mail.txt

foreach($user in $username)
{
 Set-ADuser -Identity $user -Company $company
}

#second

foreach($emailadress in $username)
{
Set-ADuser -Identity $user -EmailAddress $emailadress
}

仍然在使用 powershell 学到很多东西,有些东西很难理解,更好看:)

提前致谢!

格,

JPA

【问题讨论】:

  • 如果您有 CSV 文件,请使用 Import-Csv 读取文件。这将生成一个或多个具有与 CSV 字段名称对应的属性的对象。
  • 好的,谢谢!我现在有这个,仍然无法正常工作,但我快到了(我希望) Import-Module activedirectory Import-Csv C:\user.csv $company = "International" foreach($user in $username) { Set- ADuser -Identity $user -Company $company } #second foreach($emailadress in $username) { Set-ADuser -Identity $user -EmailAddress $emailadress }

标签: powershell csv import


【解决方案1】:

我在这里摇摇欲坠,因为我以前从未使用过 AD 命令,但它应该是这样的:

Import-Module activedirectory

$company = "International"
$users = Import-Csv c:\user.csv
$users # dumps users to allow visual inspection
read-host "press Enter to continue or Ctrl+C to abort"
$users | Foreach {Set-ADUser -Identity $_.username -Company $company -whatif}
$users | Foreach {Set-ADUser -Identity $_.username -EmailAddress $_.emailaddress -whatif}

当您认为命令将正常工作时,删除-WhatIf 参数。

【讨论】:

  • 感谢您的回答基思!还是不行,太惨了。检查了我的 CSV 文件,这似乎是正确的。思考思考...
  • “仍然不起作用”.....您可能希望使用出现的错误消息更新您的问题。此外,由于基思目前正在盲人工作,因此 csv 的样本会很棒。问题中的原始 csv-sample 不是 csv-sample。
  • 对不起! CSV 看起来像这个用户名 emailadress cursus1 cursus1@test.org cursus2 cursus2@test.org cursus3 cursus3@test.org 当我执行“Import-CSV”cmd 时,它在控制台中看起来不错,我没有收到任何错误。但是当我运行脚本时,它只在 emailadress 字段的 cursus3 帐户中添加“cursus3”(没有 mailadress,只有 cursus3)。
  • 真的是emailadress 只有一个d吗?如果是这样,将上面的行调整为$_.emailadress
  • 是的,有一个“D”,我收到此错误> Set-ADUser:找不到与参数名称“EmailAdress”匹配的参数。在 line:8 char:52 + $users | Foreach {Set-ADUser -Identity $_.username -EmailAdress $_.emailadress - ... + ~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
猜你喜欢
  • 2016-01-29
  • 2020-11-20
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
相关资源
最近更新 更多