Powershell – Computer nach OS in unterschiedliche OUs verschieben

Mit diesem Skript und einem geplanten Task, verschieben wir Computer in die jeweilige Ziel OU.

# Erstellt von Jörn Walter
# https://www.der-windows-papst.de
clear
$Server2016 = "Windows Server 2016 Standard"
$Server2012 = "Windows Server 2012 Standard"
$Server2012R2 = "Windows Server 2012 R2 Standard"
$Windows10 = "Windows 10 Pro"
$Windows8 = "Windows 8.1 Pro"
$Windows7 = "Windows 7 Pro"

$DestinationSVR = "OU=Server,OU=Machines,OU=ORG,DC=ndsedv,DC=de"
$DestinationWKS = "OU=Clients,OU=Machines,OU=ORG,DC=ndsedv,DC=de"
$DestinationDB = "OU=Database,OU=Machines,OU=ORG,DC=ndsedv,DC=de"

$machines = Get-ADComputer -Filter * -SearchBase "CN=Computers,DC=ndsedv,DC=de" -Properties OperatingSystem,Description -ErrorAction SilentlyContinue

foreach ($machine in $machines) {

if ($machine.OperatingSystem -match "$Server2016" -and $machine.Description -match "Failover") {
    $DN = $DestinationDB
    }
    elseIF ($machine.OperatingSystem -match $Server2016 -or $machine.OperatingSystem -match $Server2012 -or $machine.OperatingSystem -match $Server2012R2) {
    $DN = $DestinationSVR
    }
    elseIF ($machine.OperatingSystem -match $Windows7 -or $machine.OperatingSystem -match $Windows8 -or $machine.OperatingSystem  -match $Windows10) {
    $DN = $DestinationWKS
    }
    Move-ADObject -Identity $machine -TargetPath $DN
    Write-Host Maschine $machine.SamAccountName wurde verschoben nach $DN -ForegroundColor Green
    }