I'm trying to set up my task sequence for SCCM to automatically add laptops to 3 Active Directory groups. I've set up a step to do this and am calling upon a Powershell script to do so. The script should be run as a network admin so I don't believe I'm having any issues with authorization however I am super new to Powershell so I believe my issue is with my syntax. My script is simple enough and all of the resources I look at seem to overcomplicate Powershell for what I need to do. Here is my script:
ADD-ADGroupMember "GroupOne" -members "$env:computername$"
ADD-ADGroupMember "GroupTwo" -members "$env:computername$"
ADD-ADGroupMember "GroupThree" -members "$env:computername$"
The $env:computername is supposed to automatically gather the computer's name which is established earlier in the task sequence and the $ following it is required to add using powershell, I've found.
Any help on this would be very much appreciated.
EDIT: I've got it working finally, below is the code I've found and used for one of the powershell scripts -
$ComputerName = gc env:computername
$isMember = new-object DirectoryServices.DirectorySearcher([ADSI]"")
$ismember.filter = “(&(objectClass=computer)(sAMAccountName=$Computername$)(memberof=CN=<CN NAME>,OU=<OU NAME>,DC=<DC NAME>,DC=<DC NAME>))”
$isMemberResult = $isMember.FindOne()
If ($isMemberResult) {exit}
else
{
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"")
$searcher.filter = “(&(objectClass=computer)(sAMAccountName= $Computername$))”
$FoundComputer = $searcher.FindOne()
$P = $FoundComputer | select path
$ComputerPath = $p.path
$GroupPath = "LDAP://CN=<CN NAME>,OU=<OU NAME>,DC=<DC NAME>,DC=<DC NAME>"
$Group = [ADSI]"$GroupPath"
$Group.Add("$ComputerPath")
$Group.SetInfo()
}