0
我已經寫了批量重命名AD組一個非常簡單的PowerShell腳本,下面是代碼PowerShell的:Outputing Sucess和錯誤
# 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
}
重新命名部分工作正常,但我有麻煩的部分是輸出。輸出字符串被寫入文件和控制檯,但是如果發生錯誤(例如,新名稱已存在),則錯誤不會寫入文件,並且寫入輸出命令仍會運行。
我想知道是否有人知道這樣做的更好方法?最終目標是輸出到控制檯並記錄是否成功重命名組,如果出錯則繼續。
在此先感謝!
哇。很簡單!這正是我所需要的。我修改了錯誤寫入輸出命令以添加舊的AD組名稱。非常感謝您的幫助! 我可以檢查我對try/catch命令的理解是否正確?如果在try塊中遇到錯誤,它會立即跳出並執行catch塊中的內容? – PhilB 2014-09-04 09:42:20