如何使用powershell在Active Directory组中添加用户

时间:2016-08-10 12:46:42

标签: powershell

我用来创建用户的代码是:

 Import-Module ActiveDirectory

 $total = 2
 for ($userIndex=0; $userIndex -lt $total; $userIndex++) 
 { 
  $userID = “{0:0000}” -f ($userIndex + 1)
  $userName = “Super.admin$userID”

  Write-Host “Creating user” ($userIndex + 1) “of” $total “:” $userName

  New-ADUser `
   -AccountPassword (ConvertTo-SecureString “admin@123” -AsPlainText -Force) `
   -City “City” `
   -Company “Company” `
   -Country “US” `
   -Department “Department” `
   -Description (“TEST ACCOUNT ” + $userID + “: This user account does not represent a real user and is meant for test purposes only”)`
   -DisplayName “Test User ($userID)” `
   -Division “Division” `
   -EmailAddress “$userName@DESMOSEDICI.local” `
   -EmployeeNumber “$userID” `
   -EmployeeID “ISED$userID” `
   -Enabled $true `
   -Fax “703-555-$userID” `
   -GivenName “Test” `
   -HomePhone “703-556-$userID” `
   -Initials “TU$userID” `
   -MobilePhone “703-557-$userID” `
   -Name “Super.Admin ($userID)” `
   -Office “Office: $userID”`
   -OfficePhone “703-558-$userID” `
   -Organization “Organization” `
   -Path  "OU=BusinessUnit,DC=Domain,DC=com" `
   -POBox “PO Box $userID”`
   -PostalCode $userID `
   -SamAccountName $userName `
   -State “VA – Virginia” `
   -StreetAddress “$userID Any Street” `
   -Surname “User ($userID)” `
   -Title “Title” `
   -UserPrincipalName “$userName@Domain.com“
 }

在我的业务单位组HR下创建。如何使用上述脚本在该组中添加用户或创建用户并将HR组分配给用户? 我试图更改-Path

-Path "CN=HR,OU=Utility,DC=DESMOSEDICI,DC=com"

但它没有用。

1 个答案:

答案 0 :(得分:2)

路径是将在其中创建帐户的组织单位(或容器)。它与组成员身份无关。

使用:

Add-ADGroupMember "CN=HR,OU=Utility,DC=DESMOSEDICI,DC=com" -Member "$userName@Domain.com"

编辑:这会在脚本的上下文中显示命令:

Import-Module ActiveDirectory

$total = 2
for ($userIndex=0; $userIndex -lt $total; $userIndex++) { 
    $userID = "{0:0000}" -f ($userIndex + 1)
    $userName = "Super.admin$userID"

    Write-Host "Creating user" ($userIndex + 1) "of" $total ":" $userName

    New-ADUser `
        -AccountPassword (ConvertTo-SecureString "admin@123" -AsPlainText -Force) `
        -City "City" `
        -Company "Company" `
        -Country "US" `
        -Department "Department" `
        -Description ("TEST ACCOUNT " + $userID + ": This user account does not represent a real user and is meant for test purposes only")`
        -DisplayName "Test User ($userID)" `
        -Division "Division" `
        -EmailAddress "$userName@DESMOSEDICI.local" `
        -EmployeeNumber "$userID" `
        -EmployeeID "ISED$userID" `
        -Enabled $true `
        -Fax "703-555-$userID" `
        -GivenName "Test" `
        -HomePhone "703-556-$userID" `
        -Initials "TU$userID" `
        -MobilePhone "703-557-$userID" `
        -Name "Super.Admin ($userID)" `
        -Office "Office: $userID"`
        -OfficePhone "703-558-$userID" `
        -Organization "Organization" `
        -Path  "OU=BusinessUnit,DC=Domain,DC=com" `
        -POBox "PO Box $userID"`
        -PostalCode $userID `
        -SamAccountName $userName `
        -State "VA – Virginia" `
        -StreetAddress "$userID Any Street" `
        -Surname "User ($userID)" `
        -Title "Title" `
        -UserPrincipalName "$userName@Domain.com"

    Add-ADGroupMember "CN=HR,OU=Utility,DC=DESMOSEDICI,DC=com" -Member "$userName@Domain.com"
}

如果您从New-ADUser收到错误,则现有脚本出现问题,新命令完全独立,必须在New-ADUser完成其工作后失效。