Link to home
Start Free TrialLog in
Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Move computers from unassigned OU

Hi Guys,

I have written the script below and I only want to move machines upon meeting one of the conditions of the switch command, however the below moves every machine. please help!

$UnassignedComputers = Get-QADComputer -SearchRoot "CN=Computers,DC=X,DC=COM"
foreach ($UnassignedComputer in $UnassignedComputers){
$Prefix = $UnassignedComputer.NAME
$Adcomputer = Get-QADComputer -Name $Prefix
$Adcomputername = $Adcomputer | select -ExpandProperty name
switch -Wildcard ($Adcomputername) {
            "lp-lon*" {$ou="OU=London Laptops,OU=London Computers,OU=London,OU=Organisation,DC=X,DC=COM"}
            "pc-lon*" {$ou="OU=London PC,OU=London Computers,OU=London,OU=Organisation,DC=X,DC=COM"}
            "lp-rom*" {$ou="OU=Laptops,OU=Romania Computers,OU=Romania,OU=Organisation,DC=X,DC=COM"}
            "lp-sing*" {$ou="OU=Singapore Laptops,OU=Singapore,OU=Organisation,DC=X,DC=COM"}
	        }
	     if ($ou -ne $null) {Move-QADObject -Identity $AdComputer.DN -NewParentContainer $ou}

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just looking at the code again. You really don't need to do this:
    $Prefix = $UnassignedComputer.NAME
    $Adcomputer = Get-QADComputer -Name $Prefix
    $Adcomputername = $Adcomputer | select -ExpandProperty name

Open in new window

You already have the computer, it's in the $UnassignedComputer variable. If you change your variable name in the loop:
foreach ($adComputer in $UnassignedComputers) {

Open in new window

And make switch base itself on the Name property:
    $ou = switch -Wildcard ($adComputer.Name) {

Open in new window

You can simply delete those three extra, completely superfluous, lines without changing anything else.