Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Powershell Moving OU after build

Brief overview:

MDT builds machines into dedicated OU.

Post build, PDQ inventory detects build, PDQ deploys apps based on Criteria.

What I'm looking for a is a way to move the machine into the correct OU.

So:

I'm halfway there by getting the computername and moving it.

$newbuild = $env:computername

get-adcomputer $newbuild | Move-ADObject -TargetPath "OU=the right damn one!"

However, this relies on RSAT tools. I can do it manually by invoking a PSSession to a DC, but of course, you can't use PSsessions in scripts. Maybe I just need some more caffeine....

0

Comments

5 comments
Date Votes
  • Hey Jeff,

    Probably you don't need it anymore, but I was looking for the exact same solution. Finally solved it as followed:

    #OU path to move to
    $path = "OU=Stock,OU=Site,OU=Computers,DC=domain,DC=Local"

    #Run PS remote session on domaincontroller
    $session = New-PSSession -computerName <domaincontroller>

    #Get the computername to move
    $computer = $env:computername

    Invoke-command { import-module activedirectory } -session $session
    Invoke-command { Get-ADComputer $Using:computer | Move-ADObject -TargetPath $Using:path } -session $session

    0
  • I can't get this to work as a PDQDeploy package.

     

    Powershell version 5 is needed

    0
  • I'm quite certain that you can't implicitly remote inside of a script, though I could be wrong. That's likely why it won't work as a PDQ Deploy package. I'll rack my brain, but everything I can think of relies on RSAT or an interactive session. Cmdlets like Add-Computer and Remove-Computer aren't going to help you in this instance I don't think. 

    Also, just so I'm clear, you are wanting to put a PC in the right spot in AD as part of your deployment process, correct?

    0
  • Strange, the package is running fine here for the last couple of months. 

    0
  • I just did this the other day, here is a VB script that works for us that moves it to an Inventory OU

    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
    strComputerDN = objSysInfo.ComputerName

    objComputer.Description = "INVENTORY"
    objComputer.SetInfo
    Set objNewOU = GetObject("LDAP://OU=Inventory,OU=Computers,OU=Managed Objects,DC=yourdomain,DC=com")
    Set objMoveComputer = objNewOU.MoveHere _
    ("LDAP://" & strComputerDN, vbNullString)

    0