【问题标题】:Moving AD Users based on -Office to OU将基于-Office的AD用户移动到OU
【发布时间】:2021-05-08 22:33:06
【问题描述】:

我正在编写一个 Powershell 脚本,以根据我的 CSV 的 $_.grade 字段中的数字将用户移动到正确的 OU。

当然,我们在 Office 领域有 1-12 级的用户。在 AD 中,我们的 OU 在 Student 下只有 Primary、Middle 和 High。

我的目标是大量导入学生(我已经做到了,而且效果很好。),然后还要根据他们的成绩来调动他们。

小学 OU 是 00-04 年级
中 OU 是 05-08 年级
高 OU 是 09-12 年级

$Import.Add_Click( {
    Import-CSV -Path $filename | ForEach {
        Set-ADUser -Identity $_.user -Description $_.desc -Office $_.grade -replace @{UserType="Student"}
    
        $Information.text = "Description and UserType have been set."

        Import-CSV -Path $filename | ForEach {Add-ADGroupMember -Identity "O365-StudentLicense" -Members $_.user}
        $Information.text = "All imported accounts now have a mailbox."
    }
})

这是我当前有效的代码。不过,没有尝试让它做我想做的事。

【问题讨论】:

    标签: powershell directory exchange-server azure-powershell


    【解决方案1】:

    您可以使用-like 通配符字符串比较运算符:

    Import-CSV -Path $filename | ForEach {
        $OU = if($_.grade -like '0[0-4]'){
            'Primary'
        }
        elseif($_.grade -like '0[5-8]'){
            'Middle'
        }
        elseif($_.grade -like '09' -or $_.grade -like '1[0-2]'){
            'High'
        }
    
        $OUPath = "OU=${OU} School,OU=Parent,DC=domain,DC=tld"
    
        Move-ADObject -Identity $_.user -TargetPath $OUPath
    }
    

    [0-4] 表示“此范围内的任何字符”,因此0[0-4] 将匹配0 后跟0123 或@ 中的任何一个987654330@

    【讨论】:

    • 完美!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2021-09-26
    相关资源
    最近更新 更多