Powershell 使用 csv 在不同的 OU 中创建 AD 用户

Powershell create AD Users in different OUs using csv

我在 Active Directory 中创建新用户时遇到问题。 我有一个 .csv 文件,我在其中指定了多个 headers "location" 之一以及用户所在的城市,现在我想在与 [= 匹配的 OU 中创建用户23=]条目。

例如:"Location" 是德累斯顿,OU 是 "OU=Users,OU=Dresden,OU=Germany,DC=acme,DC=com"

位置为 "Munich" 时相同 "OU=Users,OU=Munich,OU=Germany,DC=acme,DC=com"

我玩过 Wildcars 等,但我的大脑似乎缺少 link,我如何创建一个变量 - 来自匹配的正确 OU 来设置正确的 -path 命令?

    $Domain   = 'dc=acme,dc=com'
$OUs = @( #what to put in here to make this to a matching variable?
  "OU=Users,OU=Munich,OU=Germany,",
  "OU=Users,OU=Munich,OU=Germany,",

)

不确定你现在是否能解决我的问题:)

弦乐

如果您的模式始终相同,您只需创建一个 "template string" 并使用字符串格式运算符 (-f) 将 csv 中的 location 值放入字符串中:

$Domain = 'dc=acme,dc=com'
$OUtmpl = "OU=Users,OU={0},OU=Germany,$Domain"

foreach($newUser in Import-Csv path\to.csv)
{
    $OU = $OUtmpl -f $newUser.location
    # create user in $OU here
}