Wednesday, 10 August 2011

Move computers to required OU

I usually use this script during a Task Sequence. It determines if the computer is a laptop or desktop and move it to one OU or another. Of course you can adapt it to your needs.

$computer = (Get-WmiObject -Class Win32_ComputerSystem).name
$type = Get-WmiObject -Class Win32_SystemEnclosure | % {$_.ChassisTypes}

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()

$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(cn=$computer)"
$result = $search.FindOne()
$computerToMove = [ADSI]$result.path

Try{
if($type -match 9 -or $type -match 10){

$computerToMove.psbase.Moveto([ADSI]"LDAP://OU=Laptops,OU=WorkStations,DC=domain,DC=com")}

else{

$computerToMove.psbase.Moveto([ADSI]"LDAP://OU=Desktops,OU=WorkStations,DC=domain,DC=com")}}
Catch{Write-Host "Failed to move computer"}



No comments:

Post a Comment