Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

powershell script to add computer account to global security group

hello

i need a script to get from 2 different OU all computer account and add them to a global security group

i will use scheduled task for this, so every time i have new computer the script add it to the group

i have AD 2008 R2.


thanks for help
Avatar of Robin CM
Robin CM
Flag of United Kingdom of Great Britain and Northern Ireland image

Add-ADGroupMember -Identity "Finance Server Group" -Members (Get-ADComputer -SearchBase "ou=Finance,ou=Servers,dc=rcmtech,dc=co,dc=uk")
Avatar of cawasaki
cawasaki

ASKER

hello,

computer are on 2 different OU, possibe to adabt command plz?
Just run the command twice :-)
Specify the second OU the second time around, but keep the group name the same. e.g.
Add-ADGroupMember -Identity "Finance Server Group" -Members (Get-ADComputer -SearchBase "ou=Finance,ou=Europe,ou=Servers,dc=rcmtech,dc=co,dc=uk")
Add-ADGroupMember -Identity "Finance Server Group" -Members (Get-ADComputer -SearchBase "ou=Finance,ou=Americas,ou=Servers,dc=rcmtech,dc=co,dc=uk")
hello,

i have this when i execute the script:

cmdlet Get-ADComputer at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Filter:
ASKER CERTIFIED SOLUTION
Avatar of Robin CM
Robin CM
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok now its good, but script do not add computer because he detect other one is already memeber, i have this error:

Add-ADGroupMember : The specified account name is already a member of the group
At line:1 char:18
+ Add-ADGroupMember <<<<  -Identity "Finance Server Group" -Members (Get-ADComputer -SearchBase ""ou=Finance,ou=Europe,ou=Servers,dc=rcmtech,dc=co,dc=uk"
 -Filter *)
    + CategoryInfo          : NotSpecified: (Finance Server Group:ADGroup) [Add-ADGroupMember], ADException
    + FullyQualifiedErrorId : The specified account name is already a member of the group,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
There's not necessarily a problem with that, does it carry on and add the remaining computers?
You can always add
-ErrorAction SilentlyContinue
on the end of each of the two lines if you don't like seeing the errors.
yes but i have test with one OU with 12 computer, 11 computer are already memeber of the group.

when i execute the script i have this error and the new computer is not added as a member
If you add the ErrorAction bit it'll "silently continue" and not bomb the entire command out.
Alternatively, if you're putting the two lines in a script, add this at the top of the script to set it for all commands in that script:
$ErrorActionPreference = "SilentlyContinue"