Issue moving computers from one OU to another


I need to move all the computer objects from a particular OU in Active Directory to another OU. I have a code that does the job

$ou = [ADSI]"LDAP://OU=SourceOU,OU=Test CC,DC=test,DC=com"

foreach ($item in $ou.psbase.Children){
	
if ($item.ObjectCategory -like '*computer*') { 
	#Write-Host $item.name


	switch($os)
	{
		"Windows XP"
		{
			$os= "Test XP"
		}

		"Windows 7"
		{
			$os= "Test W7"
		}
		"Windows 2000"
		{
			$os= "Test W2K"
		}
		"Windows Vista"
		{
			$os= "Test Vista"
		}
					
	}

	$new_ou="OU=" + $os + ",OU=Test CC,DC=test,DC=com"
				
	
	Move-QADObject $item.name.tostring() -To $new_ou

				
}

Open in new window


The problem is that some computer objects have a corresponding user object in AD (autologin) and when I call Move-QADObject procedure it picks up the user objects and I get "Access denied" error. Can somebody help?
LVL 35
YZlatAsked:
Who is Participating?
 
Chris DentPowerShell DeveloperCommented:
Give Move-QADObject a unique ID instead:
Move-QADObject $item.Get("distinguishedName") -To $new_ou

Open in new window

You know you could do this with Get-QADComputer instead, right? Doesn't make a lot of difference, but would be more consistent.

Cheers,

Chris
0
 
YZlatAuthor Commented:
How do I do this with Get-QADComputer?
0
 
Chris DentPowerShell DeveloperCommented:
Pretty much the same way really, just makes it a little bit neater (perhaps).

Chris
Get-QADComputer -SearchRoot "OU=SourceOU,OU=Test CC,DC=test,DC=com" -SearchScope OneLevel | ForEach-Object {
  Switch ($_.OperatingSystem) {
    "Windows XP"    { $os = "Test XP" }
    "Windows 7"     { $os= "Test W7" }
    "Windows 2000"  { $os= "Test W2K" }
    "Windows Vista" { $os= "Test Vista" }
  }

  $_ | Move-QADObject -To "OU=$os,OU=Test CC,DC=test,DC=com"
}

Open in new window

0
 
YZlatAuthor Commented:
Worked like a charm! Thank yoU!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.