Mobile Ad's Computer to the PowerShell script for the corresponding OU

Source: Internet
Author: User
<span id="Label3"> </p><pre class="brush:csharp;gutter:true;">#//*************************************************************#//editor: #//edit unit: #//edit function: move computer to the corresponding OU #// Compilation Time: 2016.01.05#//*************************************************************#************ Gets the current script execution directory $ Location = $PSScriptRoot #********************** Create log folder with Yyyy-mm-dd $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 "start Create log folder ...---------------"-foregroundcolor Green new-ite M-path $Location-name $folderName-itemtype "directory" write-host "create log folder complete ...---------------"-foregroundcolor green}#************************** Create a log file Yyyy-mm-dd.txt$datetimenow = Get-date-format ' yyyy-mm-dd ' $logFileName = $ Datetimenow.tostring () + ". txt" #************************** Create log file $logfilepath = $folderPath + "\" + $logFileName; Test-path $logFilePath)-eq $False) {write-host "start creating log files ...---------------"-foregroundcolor Green new-item-path $fol Derpath-name $logFiLename-itemtype "file" write-host "create log file complete ...---------------"-foregroundcolor green}#************** Import the PowerShell execution module for AD import-module activedirectory#************** read the computer file txt (one line in format) $computerObjects = get-content C : \temp\test.txt #************* to move the computer to the destination of Ou$targetoupath = "ou=test1,dc=contoso,dc=com" #************* Get Service Name $ ServerName = $env: computername#************* starts looping computer files Add-content-path $logFilePath-value "********************* Start the PowerShell move Operation ************************************************** "#****************** Loop foreach ($computerObject in $computerObjects) {#**************** printing information $PrintStart = "moving computer" "+ $computerObject +" " Operation! "write-host $PrintStart-foregroundcolor Green #*************** start analysis execution try {#******************** get source O U $SrcOUPath = get-adcomputer $computerObject |select distinguishedname-expandproperty distinguishedname # Print out the information $PrintOk = "the computer is being put:" "+ $computerObject +" "from the originalOu: "+ $SrcOUPath +" "move to target ou:" "+ $TargetOUPath +" "under" write-host $PrintOk-foregroundcolor Green #******** Get the computer GUID that you want to move and move it to the corresponding OU get-adcomputer $computerObject | Move-adobject-targetpath $TargetOUPath #********************** Record the correct log information for the move "$logConent = (get-date). DateTime.ToString () + "success: in computer name:" "+ $serverName +" "" computer, put the ad in the computer "" + $computerObject + "" from the original ou: "" + $SrcOUPath + "" Successfully moved to the target ou: "" + $TargetOUPath + "" under "#********************* write log Add-content-path $logFilePath-value $logCon ent} Catch {#************************* print error message $PrintError = "mobile computer" "+ $computerObject +" "in ad does not exist , contact your ad administrator to check! "write-host $PrintError-foregroundcolor Red #************************ Logging error log Information $FailContent = (get- Date). DateTime.ToString () + "failed: get operation on computer named:" "+ $serverName +" ", Unable to get information to computer" + $computerObject + "in ad, Contact AD administrator!" #************************ Write failure log Add-content-path $logFilePatH-value $FailContent}}#**************************************** executed Add-content-path $logFilePath-value "********* Perform PowerShell move operation completed ************************************************** "</pre><p><p></p></p><p><p>Mobile Ad's Computer to the PowerShell script for the corresponding OU</p></p></span>

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.