A co-worker needed to add a specified user to the local administrators group to all the servers in a specific Organizational Unit (OU), across 3 different sub-domains. Because of the different domains and service account names the script required that I prompt the user for input. After several hours (I am still very much learning PowerShell), this is what I came up with, and it worked like a charm. Any suggestions/recommendations are greatly appreciated.
This blog was extremely helpful in piecing together the last parts that I needed.
$Domain = Read-Host 'What is the domain? Ex: DOMAIN'
$ServiceAcct = Read-Host 'What is the service account? EX: TEST'
$FullDomain = "$Domain.COM"
$ComputerList = Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=$Domain,DC=COM" -Server "$Domain.COM" |
$objUser = [ADSI]("WinNT://$Domain/$ServiceAcct")
$objGroup = [ADSI]("WinNT://$Server/Administrators")