0 votes

Hello,

is there an "easy" way to add a user to all group within his OU or we need to use a script for that ?

by (460 points)
0

In fact it was pretty easy to do that with a really short script :

Import-Module Adaxes
Import-Module ActiveDirectory

#retrieve OU from user DN
$ou = "%distinguishedName%" -replace '^(.*?,OU)', 'OU'

#Add user DN to all group within $ou
$group = Get-ADGroup -SearchBase $ou -Filter * | Add-ADGroupMember -Members "%distinguishedName%"
0

Some times the script fails with the error directory object not found.

I think this is because my script may attempd to check the new user on a DC which is not replicated yet, and the information about the new utilisateur is missing.

Is it possible to retrieve the name of the DC on which the user are created ? (so i would be able to specify the DC in the script)

1 Answer

0 votes
by (216k points)
selected by
Best answer

Hello Alexandre,

The following script will do the job:

Import-Module Adaxes

$domain = $Context.GetObjectDomain("%distinguishedName%")
$rootDse = $Context.BindToObject("Adaxes://$domain/rootDSE")
$dc = $rootDse.Get("dnsHostName")

Get-AdmGroup -SearchBase "%adm-ParentDN%" -Filter * -Server $dc | Add-AdmGroupMember -Members "%distinguishedName%" -Server $dc
0

Hi Adaxes,

It seems it is working properly now. Thanks.

Related questions

0 votes
1 answer

How can I create a script that does these things For internal audit. objective Even removing all groups of a disconnected user, we will still know which groups the ... in the created group (audit)-sAMAccountName-access add the (user)-sAMAccountName in members

asked Jul 2, 2022 by alancardoso (40 points)
0 votes
1 answer

Hi Guys, I'm trying to clean all users from Local Group test_group, and next new bunch of users form TXT fiel, by executing the following script as a scheduled task Import-Module ... .txt I have only 600 users. Do you have any idea how to improve this script?

asked Feb 27, 2015 by axmaster (510 points)
0 votes
1 answer

A little bit of context: There are 3 departments that share 1 Active Directory. Now each department has its own OU. I would like to have an email sent when a user is ... if this is possible without Powershell? If not, is there a pre-existing script for this?

asked Oct 3, 2023 by Cas (200 points)
0 votes
1 answer

I've looked at https://www.adaxes.com/script-repository/copy-group-membership-from-specified-user-s590.htm. is there away to change from group names to a group type? Like exclude all distribution groups?

asked Dec 4, 2023 by Derek.Axe (480 points)
0 votes
0 answers

Hi All, I have setup an action to cancel all meetings in outlook when a user is leaving. We use Exchange online but I'm getting an error 'The operation is supported by Exchange online and Exchange versions starting from 2019' I cant see where you specify.

asked Sep 6 by DarrenFisk (60 points)
3,588 questions
3,277 answers
8,303 comments
548,097 users