星空网 > 软件开发 > ASP.net

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

#//*************************************************************#//编辑人:#//编辑单位:#//编辑作用:移动计算机到对应的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移动操作完毕**************************************************" 

 




原标题:移动AD的计算机到对应的OU的powershell脚本

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

国际快快递:https://www.goluckyvip.com/tag/101161.html
白云物流国际货站:https://www.goluckyvip.com/tag/101162.html
中日国际物流:https://www.goluckyvip.com/tag/101163.html
17track国际物流官网:https://www.goluckyvip.com/tag/101164.html
澳盟国际物流:https://www.goluckyvip.com/tag/101165.html
百达通国际物流:https://www.goluckyvip.com/tag/101166.html
西安到都江堰旅游攻略有什么?:https://www.vstour.cn/a/407236.html
9月张家界有什么好玩的地方 张家界有几个好玩的地方:https://www.vstour.cn/a/407237.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流