extsupport
asked on
Powershell: Adding users to group called "test" if they are in an OU starting with name "RBCO"
I am looking for a Powershell script that will add users to a group called test, if the user is an OU starting with the name "RBCO"
ASKER
Not working yet. Thank you
Get-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' requi
red by parameter 'SearchBase'. Specified method is not supported.
At line:1 char:42
+ $Users = Get-ADUser -Filter * -SearchBase <<<< $Search | select name, samacc
ountname, DistinguishedName
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBind
ingException
+ FullyQualifiedErrorId : CannotConvertArgument,Micr osoft.Acti veDirector y.
Management.Commands.GetADU ser
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argume
nt is null or empty. Supply an argument that is not null or empty and then try
the command again.
At line:3 char:60
+ Add-ADGroupMember -Identity $group -Members <<<< $user
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterB
indingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iv
eDirectory.Management.Comm ands.AddAD GroupMembe r
Get-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' requi
red by parameter 'SearchBase'. Specified method is not supported.
At line:1 char:42
+ $Users = Get-ADUser -Filter * -SearchBase <<<< $Search | select name, samacc
ountname, DistinguishedName
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBind
ingException
+ FullyQualifiedErrorId : CannotConvertArgument,Micr
Management.Commands.GetADU
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argume
nt is null or empty. Supply an argument that is not null or empty and then try
the command again.
At line:3 char:60
+ Add-ADGroupMember -Identity $group -Members <<<< $user
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterB
indingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidatio
eDirectory.Management.Comm
I have just made a slight modification to the script and i have tested this in my lab and it works without any issues.
Note: make sure that if you are running this script from the powershell console you need to copy and paste the ENTIRE script into NOTEPAD and save the file as filename.ps1. You will then need to open the powershell console and navigate to the location where you saved the script and type .\filename.ps1.
Or you can simple copy the entire script and paste it into the Powershell ISE and run it there.
Will.
import-module activedirectory
$Group = "test"
$Search = Get-ADOrganizationalUnit -Filter 'name -like "RBCO*"'
$Users = Get-ADUser -Filter * -SearchBase $Search
ForEach ($user in $users)
{
$user.sAMAccountName
Add-ADGroupMember -Identity $group -Members $user.sAMAccountName
}
Note: make sure that if you are running this script from the powershell console you need to copy and paste the ENTIRE script into NOTEPAD and save the file as filename.ps1. You will then need to open the powershell console and navigate to the location where you saved the script and type .\filename.ps1.
Or you can simple copy the entire script and paste it into the Powershell ISE and run it there.
Will.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Will is a very hard worker and is willing to help and assist anytime.
ASKER
Is it easy to do a script that removes users that are no longer in the RBCD OUs?
Should be easy enough.
Open a new question and send me the link I will try and assist.
Will.
Open a new question and send me the link I will try and assist.
Will.
Open in new window
Will.