Moving computer object

I need to write a function that will take as parameters computer name and destination OU and find computer with that name in AD and move it to destination OU. Can anyone help?

The function i created below I tried to test with computer name that I know is in AD. It writes to the screen "Computer found" but then does not output anything else and does not do the move. What happens is it returns $comp.Properties.distinguishedName as a blank string

   
function MoveComputerToOU($compName,$destOU )
{
	$root=[ADSI]''

	$searcher = new-object System.DirectoryServices.DirectorySearcher($root)

	$searcher.filter = "(&(objectClass=computer)(cn= $compName))"

	$comp=$searcher.findone()
	
	if($comp -ne $null){
		"computer found"
		Write-Host $comp.Properties.distinguishedname		         $dname = $comp.Properties.distinguishedName

		$MoveToOU = [ADSI]($destOU)

		$AComp = [ADSI]("$dname")

		$AComp.PSBase.moveto($MoveToOU)
	}
}

Open in new window

LVL 35
YZlatAsked:
Who is Participating?
 
GovvyCommented:
See http://www.petri.co.il/forums/showthread.php?t=31201 or Quest Powershell:

Get-QADComputer machine-name* | Move-QADObject -to domain.name/ou/subOU

0
 
YZlatAuthor Commented:
The link you provided is for VBScript
0
 
GovvyCommented:
Yes and I said or Powershell with the command
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
YZlatAuthor Commented:
I tried your suggestion and got unauthorized access error
0
 
chrismerrittCommented:
I fixed your script up for you, and included some error handling as well, i.e. it will check if it finds a computer, and will also check if the destination OU path exists before attempting the move.

Usage

Call the function similar to this, the function accepts arguments in a similar fashion to your original one I just renamed them a bit.

$CompName = "HIPPOSMBX01"
$DestOU = "OU=Test Moves,DC=hippos,DC=local"

MoveComputerToOU $ComputerName $DestinationOU

Placement of Function code

Function code must sit above all other code in any script code you use as it's read in order of placement.

On to the function!

Function MoveComputerToOU
{
	param
	(
	$ComputerName,
	$DestinationOU
	)
	
	$Root = [ADSI]''
	
	#Search for Entry, always returns one result
	$Searcher = New-Object System.DirectoryServices.DirectorySearcher($Root)
	$Searcher.Filter = "(&(objectClass=Computer)(cn=$ComputerName))"
	[array]$SearchResults = $Searcher.FindOne()
	
	#If result count is 1 then an entry has been found
	if ($SearchResults.Count -eq 1)
	{
		Write-Host -ForeGroundColor "Yellow" "Computer Found : $($SearchResults[0].Path)"
		
		#Appends LDAP:// if DestinationOU does not start with LDAP://
		if (!$DestinationOU.StartsWith("LDAP://"))
		{
			$DestinationOU = "LDAP://" + $DestinationOU
		}
		
		if ([ADSI]::Exists($DestinationOU))
		{
			$DestinationOUADSI = [ADSI]$DestinationOU
			$ComputerADSI = [ADSI]($($SearchResults[0].Path))
			
			$MoveError = 0
			
			Try
			{
				$ComputerADSI.PSBase.MoveTo($DestinationOUADSI)
			}
			Catch [Exception]
			{
				$MoveError = 1
				Write-Host -ForegroundColor "Red" -BackGroundColor "Black" $($_.Exception)
			}
			Finally
			{
				if ($MoveError -eq 0)
				{
					Write-Host -ForeGroundColor "Yellow" "Computer moved successfully"
				}
				else
				{
					Write-Host -ForegroundColor "Red" -BackGroundColor "Black" "Computer move failed"
				}
			}
		}
		else
		{
			Write-Host -ForegroundColor "Red" -BackGroundColor "Black" "Path does not exist $DestinationOU"
		}
	}
	else
	{
		Write-Host -ForegroundColor "Red" -BackGroundColor "Black" "No Results Found that match Computer Name $ComputerName"
	}
	
}

Open in new window

0
 
YZlatAuthor Commented:
chrismerritt, your function worked, but how do I prevent all the extra output to the screen? It writes all the computer data out before moving th coimputer
0
 
YZlatAuthor Commented:
Opps, sorry, i made a mistake, a different thing worked. I realized I was not calling function provided by chrismerritt.

Govvy's code worked when I ran the script with elevated permissions.

0
 
YZlatAuthor Commented:
I needed elevated permissions to run that code. Otherwise it worked fine. 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.