Thursday, October 1, 2009

Use PowerShell ADSI provider to add domain group to Local Admin group

ha ha, as i am not Domain administrator so not using much script to add user. This morning have one task need me to add some security team  to 5 server, so thiking to try about the powershell. so below is the simple script have created : (is not perfect but is working fine :) )
*servers.txt is all your servers name going to add in.

$serverList = 'C:\servers.txt'
$names = get-content $serverList
$Admins = [ADSI] 'WinNT:///'
foreach ($name in $names)
{
Write-Host 'Adding Admins to' $name
#get the local group
$localAdminGroup = [ADSI]("WinNT://$name/Administrators")
#add the global group to the local group
$localAdminGroup.Add($Admins.PSBase.Path)
}


This script have a bug , that is once the group is available at the target server then will stop and give error :). ha ha , either u write the if statement to catch it or just make sure all your server is not exist the name you want to add . ha ha..is simple, but is very fun for me to find out it.

No comments:

Post a Comment