1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
|
<#.synopsisMove AD Computer Object to specified OU.parameter ComputerNameName of computer(s) to move.parameter PathText file(s) listing computers to move. Can contain blank lines and '# comments'.parameter OUTarget OU.exampleMove-ADComputerToOU os2jp100med04ComputerName OU Success------------ -- -------OS2JP100MED04 OU=DECOMM,OU=Non_Production True.exampleMove-ADComputerToOU -Path .\jp1med.txt -OU Decomm ComputerName OU Success------------ -- -------TY1JP100MED04 OU=DECOMM,OU=Non_Production TrueTY1JP100MED05 OU=DECOMM,OU=Non_Production TrueTY1JP100MED06 OU=DECOMM,OU=Non_Production True#>[cmdletBinding()]param ( [parameter(ValueFromPipeLine=$true,Position=0)] [string[]]$ComputerName, [string[]]$Path, [parameter(Mandatory=$true,Position=1)] [string]$OU)function Move-ADComputerToOU{ [cmdletBinding()] param ( [parameter(ValueFromPipeLine=$true,Position=0)] [string[]]$ComputerName, [string[]]$Path, [parameter(Mandatory=$true,Position=1)] [string]$OU ) begin { $ErrorActionPreference = 'stop' Import-Module ActiveDirectory [System.Collections.ArrayList]$toDo = @() try { $ouObject = Get-ADOrganizationalUnit -LDAPFilter "(name=$ou)" $ouDN = $ouObject.DistinguishedName } catch { Write-Warning $_.Exception.Message break __out } } process { $Path | ? { $_ } | % { if (Test-Path -Path $Path) { (Get-Content -Path $Path) -replace '\s' -replace '#.*' | ? { $_ } | % { $ComputerName += $_ } } else { Write-Warning "-Path '$Path' not found." } } $ComputerName | ? { $_ } | % { try { $toDo += ( Get-AdComputer -Identity $_ ).DistinguishedName } catch { Write-Warning "-ComputerName '$_' not found." } } } end { $todo | Move-ADObject -TargetPath $ouDN $todo -replace ',.*' -replace '.*=' | Get-ADComputer | Select-Object @{ n = 'ComputerName' e = { $_.Name } }, @{ n = 'OU' e = { $_.DistinguishedName -replace ',DC=.*' -replace '^CN=[^,]+,' } }, @{ n = 'Success' e = { [bool]($_.DistinguishedName -match ",OU=$ou") } } }}if ($ComputerName -or $Path){ Move-ADComputerToOU @PSBoundParameters}
|