SCCM 2012 / Add computer to AD Group during build

Setup: SCCM 2012, OSD Task Sequence, Windows 7 x64

During the build after the computer is named, joined to domain, and processed whatever software it needs, I want to add the computer to an AD group for policies.

Here’s the AddComputer2ADGroup.ps1 I have today:
$ComputerName = gc env:computername
$GroupPath = “LDAP://CN=UAG DirectAccess Enabled Computers,OU=GPO Groups,OU=IT,DC=DCName,DC=com”
$computerPath = “LDAP://CN=$ComputerName,OU=Location Workstations,OU=Corporate Office,DC=DCName,DC=com”
$Group =[ADSI]"$GroupPath"

If I run these commands line by line it works
if I run the ps1 file nothing happens

I’ve ran it with the same credentials as I do when running it line by line (so I know the rights are there)

Any thoughts? I’ve been banging my head for 2 days…

Its not the cause of your problem but you could get the computer name in a simpler manner
$computername = $env:COMPUTERNAME

I’m not sure that you need the quotes on these 2 lines
$Group =[ADSI]“$GroupPath”

as you’ve already defined the strings

Your code looks right - I’d suggest putting some debug code into the script to dump the contents of $grouppath and $computerpath so you can see that they are correctly formatted

So I figure it out I did change the script to make those couple of changes you suggested however it was a SCCM trick I needed to do…
I had to run it like this:
powershell -executionpolicy bypass -File AddSuperComputer2ADGroup.ps1

I had a step to set the ExecutionPolicy to Unrestricted which didn’t take thus the script blowing up

Thank you!!!

Ron, do you have a copy of the final PowerShell script, I trying to do the same. but no good at PS. Could you send me a copy