Monday, February 17, 2014

Add Computers to a Group – PowerShell V1.0 Script

This PowerShell script can be used to add a computer to security group in Active Directory.  I have used the same logic in this Update Group Membership–PowerShell Script also.

Script:

image

Input file (computers.csv) contain computer names in the following format:

image

6 comments:

I am trying to use this script. I created a csv file called Sophos Update. I adjusted the script to reflect the file location of the file and also the AD environment to which I am trying to query and add the computer accounts to a group called Sophos Update. However it wont work. I am just seeing the code showing up on the screen and the computer accounts are not been added.

Here is the adjustments I made

#
#
#
#
Clear
$InputfileC = "C:\Scripts\Sophos Update.csv"
$OutputFile = New-item -type file -Name ("Output1_$(Get-Date -f MM-dd-yyyy_hh-mm-ss).csv") -force
"sAMAccountName`t`tCurrentValue" | Out-File $OutputFile -append -encoding ASCII
$CGroupName = [ADSI]"LDAP://CN=Sophos Update,OU=Groups,DC=corp,DC=byramhealthcare,DC=com"
{
Import-CSV $Inputfilec | % {
$ComputerN = $_.ComputerName
$ComputerN = $ComputerN + "$"
$ObjFilter = "(&(Objectclass=Computer)(samaccountName=$ComputerN))"
$objSearch = New-Object System.DirectoryServices.DirectorySearcher
$objSearch.PageSize = 5000
$objSearch.Filter = $ObjFilter
$objSearch.SearchRoot = "LDAP://DC=corp,DC=byramhealthcare,DC=com"
$AllObj = $objSearch.FindOne()
$objItemS = $AllObj.Properties
$AllObj
$CompDN = $objItemS.distinguishedname
$Comp = [ADSI] "LDAP://$CompDN"

Write-Host "Updating Appsense Group Membership. Adding Computer $ComputerN " -Nonewline
If ($CGroupName.IsMember($Comp.AdsPath) -eq $False)
{
$CGroupName.Add($Comp.AdsPath)
"$ComputerN`t`tUpdated" | Out-File $OutputFile -append -encoding ASCII
Write-Host "Done!" -ForegroundColor Blue
}
Else
{Write-Host "Already Exist"-ForegroundColor Red
"$ComputerN`t`tAlready Exist" | Out-File $OutputFile -append -encoding ASCII
}


}

}

Here is the adjustments I made

#
#
#
#
Clear
$InputfileC = "C:\Scripts\Sophos Update.csv"
$OutputFile = New-item -type file -Name ("Output1_$(Get-Date -f MM-dd-yyyy_hh-mm-ss).csv") -force
"sAMAccountName`t`tCurrentValue" | Out-File $OutputFile -append -encoding ASCII
$CGroupName = [ADSI]"LDAP://CN=Sophos Update,OU=Groups,DC=corp,DC=byramhealthcare,DC=com"
{
Import-CSV $Inputfilec | % {
$ComputerN = $_.ComputerName
$ComputerN = $ComputerN + "$"
$ObjFilter = "(&(Objectclass=Computer)(samaccountName=$ComputerN))"
$objSearch = New-Object System.DirectoryServices.DirectorySearcher
$objSearch.PageSize = 5000
$objSearch.Filter = $ObjFilter
$objSearch.SearchRoot = "LDAP://DC=corp,DC=byramhealthcare,DC=com"
$AllObj = $objSearch.FindOne()
$objItemS = $AllObj.Properties
$AllObj
$CompDN = $objItemS.distinguishedname
$Comp = [ADSI] "LDAP://$CompDN"

Write-Host "Updating Appsense Group Membership. Adding Computer $ComputerN " -Nonewline
If ($CGroupName.IsMember($Comp.AdsPath) -eq $False)
{
$CGroupName.Add($Comp.AdsPath)
"$ComputerN`t`tUpdated" | Out-File $OutputFile -append -encoding ASCII
Write-Host "Done!" -ForegroundColor Blue
}
Else
{Write-Host "Already Exist"-ForegroundColor Red
"$ComputerN`t`tAlready Exist" | Out-File $OutputFile -append -encoding ASCII
}


}

}

This comment has been removed by a blog administrator.

Thanks Paul Drummond for providing your version of the script.

This comment has been removed by a blog administrator.

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More