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 (100 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
1 answer

Hi All, I am currently using the 30 day free trial of Adaxes and seeing if we can use it to achieve our method of user provisioning. I am looking into server-side ... variable value within an SQL query Can this be achieved? Any help is much appreciated, Thanks

asked Feb 1 by Lewis (40 points)
3,326 questions
3,026 answers
7,727 comments
544,678 users