Move the AD computer to the corresponding OU powershell script, oupowershell
#//************************************* * *********************** # // Edited: # // Editing Unit: # // editing function: Move the computer to the corresponding OU # // preparation time: 2016.01.05 #//************************************ ************************ # ************* get the current script Execution directory $ Location = $ PSScriptRoot # ************************* create log folder $ folderName = "Log" # ********************* full path $ folderPath = $ Location + "\ "+ $ folderName # ********************** if the root folder does not exist. Create the root folder If (Test-Path $ folderPath)-eq $ False) {Write-Host "to create the log folder... --------------- "-ForegroundColor Green New-Item-path $ Location-name $ folderName-itemType" directory "Write-Host" after creating the log folder... --------------- "-ForegroundColor Green} # ************************** create a log file yyyy-mm-dd.txt $ DateTimeNow = Get-Date -Format 'yyyy-MM-dd' $ logFileName = $ DateTimeNow. toString () + ". txt "#********************** * *** Create a log file $ logFilePath = $ folderPath + "\" + $ logFileName; If (Test-Path $ logFilePath)-eq $ False) {Write-Host "starts to create a log file... --------------- "-ForegroundColor Green New-Item-path $ folderPath-name $ logFileName-itemType" File "Write-Host" after creating the log File... --------------- "-ForegroundColor Green} # ***************** Import AD PowerShell execution Module Import-Module ActiveDirectory #******** * ***** read the computer file TXT in one row) $ computerObjects = Get-Content c: \ Temp \ Test. TXT # *************** the OU $ TargetOUPath = "OU = test1, DC = contoso, DC = com "# ************* get the service name $ serverName = $ env: COMPUTERNAME # ************** Add-Content-Path $ logFilePath-Value "********* * ********************************* start to execute PowerShell mobile operations ** **************************************** * ******** "# ******************** loop ForEach ($ computerObject in $ computerObjects) {# **************** Print the information $ PrintStart = "in the mobile computer [" + $ computerObject + "] operation! "Write-Host $ PrintStart-ForegroundColor Green # ****************** start the analysis and execute try {#********* * ********* Get the source OU $ SrcOUPath = Get-ADComputer $ computerObject | select DistinguishedName-ExpandProperty DistinguishedName #********** * ******** print out the information $ PrintOk = "the computer is being put: ["+ $ computerObject +"] from the original OU: ["+ $ SrcOUPath +"] to the target OU: ["+ $ TargetOUPath +"] "Write-Host $ PrintOk-ForegroundColor Green #****************** * *** Get the GUID of the computer to be moved, and Move to the corresponding OU Get-ADComputer $ computerObject | Move-ADObject-TargetPath $ TargetOUPath #******************** ** correct log information recorded for moving: $ logConent = (Get-Date ). dateTime. toString () + "success: on the computer named [" + $ serverName + "], replace the computer [" + $ computerObject + "] in the AD with the original OU: ["+ $ SrcOUPath +"] is successfully moved to the target OU: ["+ $ TargetOUPath +"] "# ************************ write log Add-Content-Path $ logFilePath-Value $ logConent} catch {#*** * ******************** Print the error message $ PrintError = "mobile computer [" + $ computerObject + "] AD does not exist, contact the Active Directory administrator! "Write-Host $ PrintError-ForegroundColor Red # ************************* record error log information $ FailContent = (Get-Date ). dateTime. toString () + "failed: the computer name is [" + $ serverName +, the computer ["+ $ computerObject +"] cannot be obtained in AD. Please contact the AD administrator! "# *********************** Write failure log Add-Content-Path $ logFilePath-Value $ FailContent }}#************************************* * ** Add-Content-Path $ logFilePath-Value "************************* ****************** ********************************"