Hi!<\/p>\n
I trie to get users from source group and sync with two other groups base on the first letter of the SamAccountname.<\/p>\n
Ex: GroupeSource sync with GroupDestiA and GroupDestiB where GroupDestiA will contain the users from the GroupSource with the SamAccountName start by the letter “a”. And the GroupDestB will contain the users from the GroupSource with the SamAccountName start whit the letter “b”.<\/p>\n
I founded on the web this beautifull script two sync two groups:<\/p>\n
Param( $TargetMembers = Get-ADGroupMember -Identity $Destination | Select-Object -ExpandProperty sAMAccountName<\/p>\n $DifferenceMembers = Compare-Object -ReferenceObject $SourceMembers -DifferenceObject $TargetMembers<\/p>\n $ProcessMembers = $DifferenceMembers | Where-Object {$_.SideIndicator -eq ‘<=’} | Select-Object -ExpandProperty InputObject<\/p>\n If ($ProcessMembers -and $PSCmdlet.ShouldProcess($Destination, “Add objects: But my need is to slip the two Destination Group.<\/p>\n Somebody can help me?<\/p>\n Tx.<\/p>","upvoteCount":4,"answerCount":5,"datePublished":"2018-04-13T19:44:14.000Z","author":{"@type":"Person","name":"stefchul","url":"https://community.spiceworks.com/u/stefchul"},"suggestedAnswer":[{"@type":"Answer","text":" Hi!<\/p>\n I trie to get users from source group and sync with two other groups base on the first letter of the SamAccountname.<\/p>\n Ex: GroupeSource sync with GroupDestiA and GroupDestiB where GroupDestiA will contain the users from the GroupSource with the SamAccountName start by the letter “a”. And the GroupDestB will contain the users from the GroupSource with the SamAccountName start whit the letter “b”.<\/p>\n I founded on the web this beautifull script two sync two groups:<\/p>\n Param( $TargetMembers = Get-ADGroupMember -Identity $Destination | Select-Object -ExpandProperty sAMAccountName<\/p>\n $DifferenceMembers = Compare-Object -ReferenceObject $SourceMembers -DifferenceObject $TargetMembers<\/p>\n $ProcessMembers = $DifferenceMembers | Where-Object {$_.SideIndicator -eq ‘<=’} | Select-Object -ExpandProperty InputObject<\/p>\n If ($ProcessMembers -and $PSCmdlet.ShouldProcess($Destination, “Add objects: But my need is to slip the two Destination Group.<\/p>\n Somebody can help me?<\/p>\n Tx.<\/p>","upvoteCount":4,"datePublished":"2018-04-13T19:44:15.000Z","url":"https://community.spiceworks.com/t/sync-users-groups-with-base-on-conditions/646170/1","author":{"@type":"Person","name":"stefchul","url":"https://community.spiceworks.com/u/stefchul"}},{"@type":"Answer","text":" Welcome!<\/p>\n If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>
\n[PSObject]$Identity,
\n[PSObject]$DestinationInt,
\n[PSObject]$DestinationExt,
\n[Switch]$Mirror
\n)
\n$SourceMembers = Get-ADGroupMember -Identity $Identity | Select-Object -ExpandProperty sAMAccountName
\n#Get-ADGroupMember<\/span> -Identity G-SourceTest | Select-Object -ExpandProperty sAMAccountName<\/p>\nr<\/code>n
t$($ProcessMembers -join \"<\/code>r
n<\/code>t”)
r<\/code>n\")) {
\nAdd-ADGroupMember -Identity $Destination -Members $ProcessMembers
\n}
\nIf ($Mirror) {
\n$ProcessMembers = $DifferenceMembers | Where-Object {$_.SideIndicator -eq ‘=>’} | Select-Object -ExpandProperty InputObject
\nIf ($ProcessMembers -and $PSCmdlet.ShouldProcess($Destination, “Remove objects:r<\/code>n
t$($ProcessMembers -join \"<\/code>r
n<\/code>t”)
r<\/code>n\")) {
\nRemove-ADGroupMember -Identity $Destination -Members $ProcessMembers
\n}
\n}<\/p>\n
\n[PSObject]$Identity,
\n[PSObject]$DestinationInt,
\n[PSObject]$DestinationExt,
\n[Switch]$Mirror
\n)
\n$SourceMembers = Get-ADGroupMember -Identity $Identity | Select-Object -ExpandProperty sAMAccountName
\n#Get-ADGroupMember<\/span> -Identity G-SourceTest | Select-Object -ExpandProperty sAMAccountName<\/p>\nr<\/code>n
t$($ProcessMembers -join \"<\/code>r
n<\/code>t”)
r<\/code>n\")) {
\nAdd-ADGroupMember -Identity $Destination -Members $ProcessMembers
\n}
\nIf ($Mirror) {
\n$ProcessMembers = $DifferenceMembers | Where-Object {$_.SideIndicator -eq ‘=>’} | Select-Object -ExpandProperty InputObject
\nIf ($ProcessMembers -and $PSCmdlet.ShouldProcess($Destination, “Remove objects:r<\/code>n
t$($ProcessMembers -join \"<\/code>r
n<\/code>t”)
r<\/code>n\")) {
\nRemove-ADGroupMember -Identity $Destination -Members $ProcessMembers
\n}
\n}<\/p>\n