1

我试图让一个脚本工作,该脚本将根据他们的显示名称组织我的活动目录帐户,因为我们所有的帐户都有他们的 OU 在他们的名字(或 subOU)中。我正在尝试使用 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}
}
4

2 回答 2

0

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

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 名称以更好地反映它们的内容。

于 2015-06-18T11:23:52.987 回答
0

您在这里遇到了一些问题

  1. 就像 Vesper 说你没有传递任何东西,Move-ADObject因此你得到的错误
  2. $DisplayNames不是名称的字符串数组,而是具有 displayname 属性的对象。这就是FYI-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}
    }
}
于 2015-06-18T11:37:21.150 回答