iT邦幫忙

0

Update AD account properties by importing csv

yuyu 2017-08-17 14:41:2416169 瀏覽

如何使用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,telephonenumber
Write-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


打完收工^^


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言