Update AD account properties by importing csv

如何使用powershell 完成对Active Directory的设定

powershell类似于linux的shell,可做自动化的工作,有了powershell可以不再手动在图形化介面(GUI)做一连串的修改,透过powershell script即可轻鬆完成

对于是powershell的新手而言,若碰过linux的bash shell会觉得容易上手,因为就是shell script的概念而已

喔对了,powershell不分大小写喔~~~~


首先,针对想要更新的AD帐号整理成一份csv档,格式大约如下:

name,sam,department,phone,office
豔阳天,sunny,系统管理科,1234,MIS
多云天,cloudy,程式设计科,5578,MIS

Target: 更新department,phone,office资讯

Import-Module ActiveDirectory$data = Import-Csv -Path C:\test\test.csv

#更新AD属性

foreach ($user in $data){   Get-ADUser -Filter "SamAccountName -eq '$($user.name)'" | Set-ADUser -Replace @{   department = $($user.department);   telephonenumber = $($user.phone);   physicalDeliveryOfficeName = $($user.office);    }}

取得当下时间,使用get-date

Get-Date -Format o | Out-File C:\test\updateAD.txt -Append

#After Resetting AD,产出txt档

foreach ($user in $data){$user = Get-ADUser -Filter "SamAccountName -eq '$($user.name)'" -Properties * | select name,samAccountname,department,physicaldeliveryofficename,telephonenumberWrite-Output $user|Out-File C:\test\updateAD.txt -Append}

以下是txt档的内容

2017-08-11T14:23:10.4828042+08:00

name : 艳阳天
samAccountname : sunny
department : 系统管理科
physicaldeliveryofficename : MIS
telephonenumber : 1234

name : 多云天
samAccountname : cloudy
department : 程式设计科
physicaldeliveryofficename : MIS
telephonenumber : 5678


打完收工^^


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章