移动AD的计算机到对应的OU的powershell脚本

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

#//*************************************************************

#//编辑人:

#//编辑单位:

#//编辑作用:移动计算机到对应的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() +"失败:在计算机名为:【" + $serverName +"】电脑上进行获取操作,在AD中无法获取到计算机【"+ $computerObject +"】的信息,请与AD管理员联系!"

#************************写入失败日志

Add-Content -Path $logFilePath -Value $FailContent

}

}

#****************************************执行完毕

Add-Content -Path $logFilePath -Value "******************************************执行PowerShell移动操作完毕**************************************************"

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航