【发布时间】:2014-09-04 09:01:55
【问题描述】:
我写了一个非常简单的 Powershell 脚本来批量重命名 AD 组,下面是代码
# This script will perform a bulk rename of AD groups.
# Takes input from a csv file called "ADGroups.csv" with two columns,
# first column labelled "ObjectGUID" and contains the GUID of the AD Group you wish to rename and
# the second column is labeled "NewName" and contains the name that you want the group to be renamed to.
# Outputs results to console and a file called ADGroupBulkRenameLog.txt
$Groups = Import-Csv ADgroups.csv
Foreach ($Group in $Groups)
{
$OldName = Get-ADGroup -Identity $Group.ObjectGUID | Select Name
Rename-ADObject -Identity $Group.ObjectGUID -NewName $Group.NewName
Set-ADGroup -Identity $Group.ObjectGUID -SamAccountName $Group.NewName
Write-Output ($OldName.Name + " has been renamed to " + $Group.NewName) | Tee-Object ADGroupBulkRenameLog.txt -Append
}
重命名部分工作正常,但我遇到问题的部分是输出。输出字符串同时写入文件和控制台,但是如果发生错误(例如新名称已存在),则错误不会写入文件并且 write-output 命令仍然运行。
我想知道是否有人知道这样做的更好方法?最终目标是输出到控制台和文件,说明组是否成功重命名,如果出错则继续。
提前致谢!
【问题讨论】:
标签: powershell logging output