如何将用户添加到多个组

时间:2016-05-03 19:45:17

标签: active-directory powershell-v3.0

我想指定一个源用户并捕获用户所属的组列表。我想指定第二个用户,并将原始捕获的组添加到新用户帐户。

如果user1是GroupA,GroupB,GroupC的成员,我希望脚本捕获该脚本,并在输入其名称时将其添加到user3。

Import-Module ActiveDirectory

$SourceUser = Read-Host -Prompt 'Enter the username of the user whos groups you would like to copy'
$DestUser = Read-Host -Prompt "Enter the username of the user who will get: $SourceUser groups"

$SourceGroups = Get-ADUser $SourceUser -Property MemberOf | % {
    $_.MemberOf | Get-ADGroup | select Name | sort name
}

"List of groups for user $DestUser BEFORE script: "
Get-ADUser $DestUser -Property MemberOf | % {
    $_.MemberOf | Get-ADGroup | select Name | sort name
}

# ***Where my problem is***
#Add-ADGroupMember $SourceGroups –Member $DestUser

"List of groups for user $DestUser AFTER script: "
Get-ADUser -Identity $DestUser -Property MemberOf | % {
    $_.MemberOf | Get-ADGroup | select Name | sort name
}

3 个答案:

答案 0 :(得分:1)

如有疑问,请阅读documentation

  

语法

     

Add-ADGroupMember [-Identity] <ADGroup> [-Members] <ADPrincipal[]> ...

-Identity参数(第一个参数)采用单个组标识,而不是标识列表。要将用户添加到组列表,您需要循环:

$SourceGroups | ForEach-Object { Add-ADGroupMember $_ –Member $DestUser }

答案 1 :(得分:0)

以下是现在有效的更正代码。

    $T2FolderExists = Test-Path "C:\T2\"
$LogsFolderExists = Test-Path "C:\T2\Logs\"

IF ($T2FolderExists -eq $False)
{

    New-Item C:\T2\  -type directory
        IF ($LogsFolderExists -eq $False)
        {
            New-Item C:\T2\Logs\ -type directory
        }
        ELSE
        {
        }
"`n" 
Start-Transcript -Path C:\T2\Logs\CopyMembershipFromADAccount.txt -Append
"`n"
}
ELSE
{
    "`n" 
    Start-Transcript -Path C:\T2\Logs\CopyMembershipFromADAccount.txt -Append
    "`n"
}

<#*****Session Paramaters*****>
Set-ExecutionPolicy Unrestricted -force
Import-Module ActiveDirectory


Clear-Host
Write-Host "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
$SourceUser = Read-Host
"`n"

Write-Host "Enter the username of the user who will get: $SourceUser groups" -foregroundcolor Green -backgroundcolor Black
$DestUser = Read-Host
"`n"

$SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object {
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}


"`n`n"
Write-Host "List of groups for user $SourceUser BEFORE script: "
Get-ADUser $SourceUser -Property MemberOf | ForEach-Object {
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}
"`n`n"
Write-Host "List of groups for user $DestUser BEFORE script: "
Get-ADUser $DestUser -Property MemberOf | ForEach-Object {
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}
"`n`n"

ForEach ($Group in $SourceGroups)
{
Add-ADGroupMember $Group –Member $DestUser
Write-Host "Adding $Group to $DestUser 's Account" -foregroundcolor Cyan -backgroundcolor Black
}
"`n`n"
Write-Host "List of groups for user $SourceUser BEFORE script: "
Get-ADUser $SourceUser -Property MemberOf | ForEach-Object {
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}
"`n`n"
Write-Host "List of groups for user $DestUser AFTER script: "
Get-ADUser $DestUser -Property MemberOf | ForEach-Object {
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}


"`n"
Stop-Transcript
Exit

答案 2 :(得分:-1)

这就是我所做的:

 class MyObject extends mojs.CustomShape {
   getShape () {
    return '..path'
   }
 };

您可以在脚本中使用更多组而不使用它们。

在&#34; data.csv&#34;中,你把它(例如):

Import-Module ActiveDirectory

Import-CSV "data.csv" -Delimiter ";" |
 ForEach {`

    $_.Group1,$_.Group2,$_.Group3,$_.Group4,$_.Group5,$_.Group6 |
        Add-ADGroupMember `
            -Members        $_.Alias`
 }

虽然我和#34;身份&#34;无法检查,它有效。