[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Moving computer object

Posted on 2011-10-21
8
Medium Priority
?
430 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:YZlat
  • 5
  • 2
8 Comments
 
LVL 13

Accepted Solution

by:
Govvy earned 2000 total points
ID: 37008084
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
 
LVL 35

Author Comment

by:YZlat
ID: 37008309
The link you provided is for VBScript
0
 
LVL 13

Expert Comment

by:Govvy
ID: 37008347
Yes and I said or Powershell with the command
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
LVL 35

Author Comment

by:YZlat
ID: 37008413
I tried your suggestion and got unauthorized access error
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 37010808
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
 
LVL 35

Author Comment

by:YZlat
ID: 37030989
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
 
LVL 35

Author Comment

by:YZlat
ID: 37031039
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
 
LVL 35

Author Closing Comment

by:YZlat
ID: 37031082
I needed elevated permissions to run that code. Otherwise it worked fine. Thank you!
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Loops Section Overview
Screencast - Getting to Know the Pipeline

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question