Monday, February 17, 2014

Home / Add Computers to a Group – PowerShell V1.0 Script

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

SS

Santhosh Sivarajan

Microsoft MVP · Identity & Cybersecurity Architect

Santhosh has 30+ years of hands-on enterprise experience in Identity and Access Management, Microsoft Entra ID, Active Directory, Microsoft 365 and Zero Trust architecture. He is the author of two books on Windows Server and security, and leads consulting, assessments and training at SAG Business Group.

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
}


}

}

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
Workstation Trust Relationship Issue Issue: You receive the following error message, when you try to login to the domain.  The security database on the server does not have ... ADMT Service Account - Permission and Configuration The ADMT service account needs to have proper permission in source and target domains.  You don’t need to use 2 separate accounts.  You can ... My First Peek into Microsoft Exchange 2010 By Santhosh Sivarajan Before I really dive into Exchange 2010, I thought I would install and play with it first. I took some screen shots and notes during the ins... ObjectSID and Active Directory What is an objectSID in Active Directory? When a new object is created in Active Directory, Domain Controller assigns a unique value used ... AD Group Report - List Group Members in Active Directory–PowerShell Script Updated Script - http://portal.sivarajan.com/2011/10/search-ad-collect-local-admin-group.html Script #1 This script... Add Users to a Group–PowerShell Script Purpose – Add users to a group from an input file – PowerShell V2 Script.  Input file – Input file (Users.csv) contains samAccountName in... User Account Migration and Merging – Part I (ADMT) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM pre-creating user account ... User Account Migration and Merging – Part II (Quest Migration Manager) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM Pre-creating user account in... Delete Stale or Inactive Computer Accounts from Active Directory Here is an easy way to identify and delete inactive or stale computers in an Active Directory environment.  Using the dsquery command you c... Converting PowerShell (PS1) to EXE / Standalone Application As we know, there many applications available to convert a PowerShell file to a standalone executable file.  Based on my experience, PowerSh...