您好,登錄后才能下訂單哦!
需求描述:
因生產環境數據接口需求,需要對某時間段已上線用戶屬性進行批量互換,涉及字段EmployeeNumber,EmployeeID
環境信息:
Windows Server 2012 R2 ActiveDirectory
操作步驟:
PS.操作之初,建議先對當前環境屬性字段進行備份導出操作。
方法一:
1.過濾出某時間段創建帳戶信息,并導出域帳號EmployeeNumber,EmployeeID字段;
$date=Get-Date $oldday=(Get-Date).AddDays(-180) Get-ADUser?-Filter?*?-Properties?*?-SearchBase?"OU=Old,OU=Staff,DC=a,DC=com"?|?{$_.whenCreated?-le?$date?-and?$_.whenCreated?-ge?$oldday}?|` Select-Object?name,samaccountname,userPrincipalName,EmployeeNumber,EmployeeID?|?` Export-Csv?C:\New0505.csv?-Encoding?UTF8?-NoTypeInformation
2.將表格數據進行按需調整,去掉ID字段為空的用戶信息,并保存CSV文本格式為utf8
3.執行導入用戶信息操作,并查看當前各字段對應信息屬性
Import-Csv?'C:\New0505.csv'?|?select?SamAccountName,EmployeeNumber,EmployeeID
4.如信息無誤,執行批量替換操作
4.1如ID與Number字段位置無調整,執行如下命令:
Import-Csv?'C:\New0505.csv'?|?ForEach-Object?{?Set-ADUser?-Identity?$_.SamAccountName?-EmployeeNumber?$_.EmployeeID?-EmployeeID?$_.EmployeeNumber?}
4.2如ID與Number字段已調整,則按照正常替換格式替換,執行如下命令:
Import-Csv?'C:\New0505.csv'?|?ForEach-Object?{?Set-ADUser?-Identity?$_.SamAccountName?-EmployeeNumber?$_.EmployeeNumber?-EmployeeID?$_.EmployeeID?}
查看替換后屬性字段信息
Import-Csv?'C:\New0505.csv'?|?ForEach-Object?{?get-ADUser?-Identity?$_.SamAccountName?-Properties?*?|select?SamAccountName,EmployeeNumber,EmployeeID?}
5.完成替換操作
方法二:
PS.如當前環境中數據均無須調整,可直接執行替換操作,具體如下:
1.以OU為例介紹,查看當前OU下用戶相關屬性:
Get-ADUser?-SearchBase?"OU=Old,OU=Staff,DC=a,DC=com"?-Filter?*?-Properties?*?|select?SamAccountName,EmployeeNumber,EmployeeID
2.執行批量互換當前屬性操作:
Get-ADUser?-SearchBase?"OU=Old,OU=Staff,DC=a,DC=com"?-Filter?*?-Properties?*?|select?SamAccountName,EmployeeNumber,EmployeeID?|ForEach-Object?{?Set-ADUser?-Identity?$_.SamAccountName?-EmployeeNumber?$_.EmployeeID?-EmployeeID?$_.EmployeeNumber?}
3.再次查看當前OU下用戶屬性,發現用戶屬性已變更完成。
Get-ADUser?-SearchBase?"OU=Old,OU=Staff,DC=a,DC=com"?-Filter?*?-Properties?*?|select?SamAccountName,EmployeeNumber,EmployeeID
綜合:對方法一腳本進行優化操作,批量互換某時間段用戶屬性字段信息:
$date=Get-Date $oldday=(Get-Date).AddDays(-180) Get-ADUser?-Filter?*?-Properties?*?-SearchBase?"OU=Old,OU=Staff,DC=a,DC=com"?|?{$_.whenCreated?-le?$date?-and?$_.whenCreated?-ge?$oldday}?|` Select-Object?name,samaccountname,userPrincipalName,EmployeeNumber,EmployeeID?| forEach-Object?{?Set-ADUser?-Identity?$_.SamAccountName?-EmployeeNumber?$_.EmployeeID?-EmployeeID?$_.EmployeeNumber?}
完成本次需求操作。
歡迎關注微信公眾號:小溫研習社
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。