Many Scenario we push GPO, based on Server Group Member Info. The below Scrips will help us to Add Servers into Specific ADGroup.
# Name of The OU, where we are going to take Input Object
$OU = "OU=DomainServer,OU=DomainObjects,DC=lab,DC=local"
# Active Directory Group,
$ADGroup = "CN=GroupA,OU=DomainGroup,OU=DomainObjects,DC=lab,DC=local"
# Get Server Information
$Servers = Get-ADComputer -Filter * -SearchBase $OU
# Check each Servers if they are part of GroupA.
foreach ($Srv in $Servers){
# Checking Server Group information
$ServerGroupMemberof = Get-ADComputer -Identity $Srv -Properties * | Select-Object MemberOf -ExpandProperty MemberOf
# Check If Server Memberof contains ADGroup
If ($ServerGroupMemberof -contains $ADGroup){
# If Server MemberShip Contains AD Group Log the Result.
$Log= "Server $Srv MembeorOf $ADGroup" |out-file -Append ".\Result.txt"
}
# IF server does not have Group Member info, it will add Group.
else {
# Adding Servers into Group
Add-ADPrincipalGroupMembership -Identity $Srv -MemberOf $ADGroup
# Storing the result in logs.
$Log = "Server $Srv Has benn MemberOF $ADGroup"|Out-File -Append ".\Result.txt"
}
}
# Name of The OU, where we are going to take Input Object
$OU = "OU=DomainServer,OU=DomainObjects,DC=lab,DC=local"
# Active Directory Group,
$ADGroup = "CN=GroupA,OU=DomainGroup,OU=DomainObjects,DC=lab,DC=local"
# Get Server Information
$Servers = Get-ADComputer -Filter * -SearchBase $OU
# Check each Servers if they are part of GroupA.
foreach ($Srv in $Servers){
# Checking Server Group information
$ServerGroupMemberof = Get-ADComputer -Identity $Srv -Properties * | Select-Object MemberOf -ExpandProperty MemberOf
# Check If Server Memberof contains ADGroup
If ($ServerGroupMemberof -contains $ADGroup){
# If Server MemberShip Contains AD Group Log the Result.
$Log= "Server $Srv MembeorOf $ADGroup" |out-file -Append ".\Result.txt"
}
# IF server does not have Group Member info, it will add Group.
else {
# Adding Servers into Group
Add-ADPrincipalGroupMembership -Identity $Srv -MemberOf $ADGroup
# Storing the result in logs.
$Log = "Server $Srv Has benn MemberOF $ADGroup"|Out-File -Append ".\Result.txt"
}
}
No comments:
Post a Comment