Move the AD computer to the corresponding OU powershell script, oupowershell

Source: Internet
Author: User

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 "************************* ****************** ********************************"

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.