SCCM 2012 / Add computer to AD Group during build

Tagged: , ,

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Willie Williams Willie Williams 8 months, 4 weeks ago.

  • Author
    Posts
  • #11132
    Profile photo of Ron Scott
    Ron Scott
    Participant

    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"
    $Group.Add("$computerPath")
    $Group.SetInfo()

    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....

  • #11135
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    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”
    $Group.Add(“$computerPath”)

    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

  • #11161
    Profile photo of Ron Scott
    Ron Scott
    Participant

    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!!!

    • #54778
      Profile photo of Willie Williams
      Willie Williams
      Participant

      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
      WIllie

You must be logged in to reply to this topic.