Powershell获取内容,foreach

时间:2016-10-27 11:41:02

标签: powershell foreach active-directory

我编写了一个Powershell脚本来提示输入Active Directory用户名,然后根据托管邮箱的电子邮件服务器将用户添加到相关的AD组。

这很好但我希望通过从文本文件导入AD用户名列表来进一步自动化。我一直在关注Get-ContentForeach,但没有取得多大成功。

有人能告诉我正确的方法吗?

$list = Get-Content "C:\users.txt"
Foreach ($user in $list) { 
    #Original code to prompt
    #$user = Read-Host -Prompt 'Input the username'
    Add-ADGroupMember -Identity "MobileIron Staff" -Member $user
    $HomeMDB = Get-ADuser $user -Properties * | Select *HomeMDB*
    if ($HomeMDB -like '*Email_Ser1*') {
        Add-ADGroupMember -Identity "Email_Server1" -Member $user
    }
    if ($HomeMDB -like '*Serv2*') {
        Add-ADGroupMember -Identity "Email_Server2" -Member $user
    }
    if ($HomeMDB -like '*Serv_Exchange3*') {
        Add-ADGroupMember -Identity "Email_Server3" -Member $user
    }
}
Write-Host "--------------------------------------------"
Write-Host $user "Exchange Mailbox Store::: $HomeMDB "
Write-Host "--------------------------------------------"
Read-Host -Prompt 'Press Enter to Exit'
exit

1 个答案:

答案 0 :(得分:0)

脚本看起来很好。我建议你以下面的格式给出输入文件(一个在另一个之下)

user1
user2
user3

希望这有帮助!