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:
Input file (computers.csv) contain computer names in the following format:
7 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
}
}
}
Thanks Paul Drummond for providing your version of the script.
Great Article
IEEE Projects on Information Security
Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai
Post a Comment