组织 Active Directory 帐户

Organizing Active Directory accounts

我正在尝试使用一个脚本来根据显示名称组织我的 Active Directory 帐户,因为我们所有帐户的名称中都有 OU(或子 OU)。我正在尝试使用 PowerShell 中 ForEach 循环内的 If 语句来执行此操作。不过,每次我 运行 它,它都会不断询问我的身份。谁能帮我解决这个问题?这就是我所拥有的...

Import-Module ActiveDirectory
$OU = "OU=Test, OU=com"
$Test1OU = "OU=Test1, OU=Test, OU=Com"
$Test2OU = "OU=Test2, OU=Test, OU=Com"

$Users = (Get-ADUser -SearchBase $OU -Filter * -Properties samAccountName,DisplayName)
ForEach ($user in $users)
{
If ($($user.DisplayName -like ("*Supply*" -or "*Supplies*"))
{Move-ADObject -Identity $($user.samAccountName -TargetPath $Test1OU}
ElseIf ($($user.DisplayName -like ("*Accounting*" -or "*Accountant*"))
{Move-AdObject -TargetPath $Test2OU}
}

我目前无法测试,但这应该可以解决问题:

Import-Module ActiveDirectory

$OU = "OU=Test, OU=com"
$Test1OU = "OU=Test1, OU=Test, OU=Com"
$Test2OU = "OU=Test2, OU=Test, OU=Com"

$users = (Get-ADUser -SearchBase $OU -Filter * -Properties displayName)
foreach ($user in $users)
{
    if ($($user.displayName) -like "*Supply*" -OR $($user.displayName) -like "*Supplies*")){
        Move-ADObject -Identity $user -TargetPath $Test1OU
    }
    elseif ($($user.displayName) -like "*Accounting*" -OR $($user.displayName) -like "*Accountant*")) {
        Move-AdObject -Identity $user -TargetPath $Test2OU
    }
}

我已经向 Move-ADObject 添加了一个标识参数,我还更改了一些 var 名称以更好地反映它们的内容。

你 运行 遇到了一些问题

  1. 就像 Vesper 所说的那样,你没有将任何东西传递给 Move-ADObject 因此你得到的错误是
  2. $DisplayNames 不是名称的字符串数组,而是具有显示名称 属性 的对象。这就是 -ExpandProperty 参数用于 Select-Object 仅供参考。
  3. 您正在拉取所有用户,但实际上只想处理某些用户。让我们使用更有针对性的方法来代替 -Filter *
  4. 虽然很诱人,但您不能嵌套 -like 这样的条件。如果您使用 "*Supply*" -or "*Supplies*" 并键入它,它将计算为真。与所有非零长度字符串相同。

对于我们计划做的事情,我们不必解决所有这些问题。我们应该使用管道来帮助解决这个问题。根据您有多少差异,像 switch 语句这样的东西可能会更好,下面将介绍。

$supplyFilter = 'DisplayName -like "*Supply*" -or DisplayName -like "*Supplies*"'
$accountFilter = 'DisplayName -like "*Accounting*" -or DisplayName -like "*Accountant*"'

Get-ADUser -SearchBase $OU -Filter $supplyFilter -Properties displayName | Move-ADObject -TargetPath $Test1OU
Get-ADUser -SearchBase $OU -Filter $accountFilter -Properties displayName | Move-ADObject -TargetPath $Test2OU

您可能会对此感到奇怪,并在循环中使用过滤器和目标对创建一个自定义对象,这样您就不需要对每个 Get-ADuser 实例重复调用 cmdlet。

$moves = @(
    @{
        Filter = 'DisplayName -like "*Supply*" -or DisplayName -like "*Supplies*"'
        OU = "OU=Test1, OU=Test, OU=Com"  
    },
    @{
        Filter = 'DisplayName -like "*Accounting*" -or DisplayName -like "*Accountant*"'
        OU = "OU=Test2, OU=Test, OU=Com"
    }
) | ForEach-Object{New-Object -TypeName PSCustomObject -Property $_}

ForEach($move in $moves){
    Get-ADUser -SearchBase $OU -Filter $move.Filter -Properties displayName | Move-ADObject -TargetPath $move.OU
}

您应该能够通过添加新的 $moves 轻松扩展到此。使用 PowerShell v3.0 会更干净,但我不知道您使用的是什么版本。

使用开关

如果您想要更接近您目前拥有的东西,那么我会建议您使用类似的东西。

$Users = Get-ADUser -SearchBase $OU -Filter * -Properties DisplayName
ForEach ($user in $users){
    switch($user.DisplayName)  {
        ($_ -like "*Supply*" -or $_ -like "*Supplies*"){Move-ADObject -Identity $user -TargetPath $Test1OU}
        ($_ -like "*Accounting*" -or $_ -like "*Accountant*"){Move-ADObject -Identity $user -TargetPath $Test1OU}
    }
}