-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcopy_security_groups.ps1
36 lines (28 loc) · 1.1 KB
/
copy_security_groups.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Param(
[Parameter(Position=0,mandatory=$true)]
[string]$SourceGroup,
[Parameter(Position=0,mandatory=$true)]
[string]$DestinationGroup
)
try{
$SourceGroupCheck = Get-ADGroup -Identity $SourceGroup
$DestinationGroupCheck = Get-ADGroup -filter {sAMAccountName -eq $DestinationGroup}
$Group = Get-ADGroupMember -Identity $SourceGroupCheck.SamAccountName
if($DestinationGroupCheck){
Write-Output "Copying users from $SourceGroup to $DestinationGroup"
foreach ($Person in $Group) {
Add-ADGroupMember -Identity $DestinationGroupCheck.SamAccountName -Members $Person.distinguishedname
}
}
else {
Write-Output "$DestinationGroup does not exist... Creating Group"
New-ADGroup -Name $DestinationGroup -Path ($SourceGroupCheck.DistinguishedName -replace '^[^,]*,','') -GroupScope Global
foreach ($Person in $Group) {
Add-ADGroupMember -Identity $DestinationGroup -Members $Person
}
Write-Output "$DestinationGroup group created and users copied"
}
}
catch{
$error[0]
}