Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Powershell- bulk swap domain user attribute information

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Requirements description:

Due to the requirement of data interface in production environment, batch exchange of user attributes that have been online in a certain period of time is required, involving field EmployeeNumber,EmployeeID

Environmental Information:

Windows Server 2012 R2 ActiveDirectory

Procedure:

PS. At the beginning of the operation, it is recommended to backup and export the current environment attribute fields.

Method 1:

1. Filter out the account creation information for a certain period of time, and export the domain account EmployeeNumber,EmployeeID field

$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

two。 Adjust the table data as needed, remove the user information whose ID field is empty, and save the CSV text format as utf8

3. Perform the operation of importing user information and view the corresponding information attributes of the current fields

Import-Csv 'C:\ New0505.csv' | select SamAccountName,EmployeeNumber,EmployeeID

4. If the information is correct, perform a batch replacement operation

4.1 if the ID and Number fields are not adjusted, execute the following command:

Import-Csv 'C:\ New0505.csv' | ForEach-Object {Set-ADUser-Identity $_ .SamAccountName-EmployeeNumber $_ .EmployeeID-EmployeeID $_ .EmployeeNumber}

4.2 if the ID and Number fields have been adjusted, replace them in the normal replacement format and execute the following command:

Import-Csv 'C:\ New0505.csv' | ForEach-Object {Set-ADUser-Identity $_ .SamAccountName-EmployeeNumber $_ .EmployeeNumber-EmployeeID $_ .EmployeeID}

View property field information after replacement

Import-Csv 'C:\ New0505.csv' | ForEach-Object {get-ADUser-Identity $_ .SamAccountName-Properties * | select SamAccountName,EmployeeNumber,EmployeeID}

5. Complete the replace operation

Method 2:

PS. If there is no need to adjust the data in the current environment, you can directly perform the replacement operation, as shown below:

1. Take OU as an example to view the relevant user attributes under the current OU:

Get-ADUser-SearchBase "OU=Old,OU=Staff,DC=a,DC=com"-Filter *-Properties * | select SamAccountName,EmployeeNumber,EmployeeID

two。 Perform a batch swap of the current attributes:

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. Check the user attributes under the current OU again and find that the user attributes have been changed.

Get-ADUser-SearchBase "OU=Old,OU=Staff,DC=a,DC=com"-Filter *-Properties * | select SamAccountName,EmployeeNumber,EmployeeID

Synthesis: optimize the method 1 script and exchange user attribute field information in batches for a certain period of time:

$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}

Complete this requirement operation.

Welcome to the official account of Wechat: Xiao Wen study Society.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report