#//*************************************************************#//編輯人:#//編輯單位:#//編輯作用:移動計算機到對應的OU下#//編制時間:2016.01.05#//*************************************************************#************獲取當前腳本執行的目錄$Location = $PSScriptRoot#**********************創建以yyyy-MM-dd的日志文件夾$folderName ="Log" #*********************全路徑$folderPath = $Location + "/" + $folderName#*********************如果根文件夾不存在。則創建根文件夾If((Test-Path $folderPath) -eq $False) { Write-Host "開始創建日志文件夾...---------------" -ForegroundColor Green New-Item -path $Location -name $folderName -itemType "directory" Write-Host "創建日志文件夾完畢...---------------" -ForegroundColor Green}#**************************創建一個日志文件yyyy-MM-dd.txt$DateTimeNow = Get-Date -Format 'yyyy-MM-dd'$logFileName = $DateTimeNow.ToString() +".txt"#**************************創建日志文件$logFilePath = $folderPath + "/" + $logFileName;If((Test-Path $logFilePath) -eq $False) { Write-Host "開始創建日志文件...---------------" -ForegroundColor Green New-Item -path $folderPath -name $logFileName -itemType "File" Write-Host "創建日志文件完畢...---------------" -ForegroundColor Green}#**************導入AD的PowerShell執行模塊Import-Module ActiveDirectory#**************讀取計算機文件TXT(格式一行一個)$computerObjects = Get-Content c:/Temp/Test.TXT #*************要移動的計算機到目標的所在的OU$TargetOUPath = "OU=test1,DC=contoso,DC=com"#*************得到服務名稱$serverName = $env:COMPUTERNAME#*************開始循環讀取的計算機文件Add-Content -Path $logFilePath -Value "******************************************開始執行PowerShell移動操作**************************************************" #******************循環ForEach($computerObject in $computerObjects){ #****************打印信息 $PRintStart = "正在移動計算機【" + $computerObject +"】操作!" Write-Host $PrintStart -ForegroundColor Green #***************開始分析執行 try { #********************得到源的OU $SrcOUPath = Get-ADComputer $computerObject |select DistinguishedName -ExpandProperty DistinguishedName #********************打印出信息 $PrintOk = "正在把計算機:【" + $computerObject + "】從原有OU:【" + $SrcOUPath + "】移動到目標OU:【" + $TargetOUPath +"】下" Write-Host $PrintOk -ForegroundColor Green #**********************得到要移動的計算機GUID,并移動到對應的OU下 Get-ADComputer $computerObject | Move-ADObject -TargetPath $TargetOUPath #**********************記錄移動的正確日志信息】 $logConent = (Get-Date).DateTime.ToString() +"成功:在計算機名為:【" + $serverName +"】電腦上,把AD里的計算機【" + $computerObject+ "】從原有OU:【"+ $SrcOUPath +"】成功移動到目標OU下:【" + $TargetOUPath +"】下" #*********************寫入日志 Add-Content -Path $logFilePath -Value $logConent } catch { #*************************打印錯誤信息 $PrintError = "移動的計算機【" + $computerObject +"】在AD不存在,請聯系AD管理員核對!" Write-Host $PrintError -ForegroundColor Red #************************記錄錯誤日志信息 $FailContent = (Get-Date).DateTime.ToString() +"失?。涸谟嬎銠C名為:【" + $serverName +"】電腦上進行獲取操作,在AD中無法獲取到計算機【"+ $computerObject +"】的信息,請與AD管理員聯系!" #************************寫入失敗日志 Add-Content -Path $logFilePath -Value $FailContent }}#****************************************執行完畢Add-Content -Path $logFilePath -Value "******************************************執行PowerShell移動操作完畢**************************************************"
新聞熱點
疑難解答