powershell小腳本--批量添加用戶屬性----導出登錄時間


需求1:某公司所有員工少了MAIL屬性,需要批量添加。例如,用戶chenyy  添加郵件屬性chenyy@xxxx.com

先導出(只導出名字)備用:

Get-ADUser -Filter *  -Properties * | select name | Export-Csv c:\test.csv

用where條件可以過濾系統賬號

Get-ADUser -Filter *  -Properties * |where {$_.UserPrincipalName -ne $null} | select name | Export-Csv c:\test.csv

*然后打開test.csv添加一個表頭,命名為User(這是沒過濾的)

 

導入並寫一個循環:

$file = Import-csv c:\test.csv

foreach ($User in $file)

{

  $username = $User.data  # $User表示這一行,$User.data表示這一行的數據,取得用戶名chenyy

  $str = "@xxxx.com"

  $mail = $username+$str  #合並字符串chenyy@xxxx.com

  Set-ADUser  -Indentity $User.data -Emailaddress $mail  #用set-aduser插入mail屬性

}

需求2:獲取用戶最近登錄時間。(表頭為User)

$Contents=Import-Csv C:\test.csv 
$Line=$Contents.Length #獲取行數(人數0開始)
Write-Output "Total users have $Line"
for ($i=0;$i -lt $Line;$i++) { $User=$Contents.User[$i] #表頭為User

Write-Output "The User is $User" #打印當前用戶
#最后登陸時間戳 $Stamp=Get-ADUser -Identity $User.data -Properties * | Select-Object name,distinguishedname,@{Name="Stamp";Expression={[system.datetime]::FromFileTime($_.lastLogonTimestamp)}} #獲取當前用戶最后登陸時間
Write-Output $Stamp >> c:\result.csv #導出為csv
}

  

========================================================================================================================================================================================================
                                    

Powershell批量修改用戶的UPN后綴

適用產品:Windows Server ActiveDirectory
查詢AD中UPN為空的用戶
Get-ADUser -Filter * -Properties * | where {$_.UserPrincipalName -eq $null} | Select-Object name,SamAccountName,UserPrincipalName
設置UPN后綴
Get-ADUser -Filter * -Properties * | where {$_.UserPrincipalName -eq $null} | Select-Object name,SamAccountName,UserPrincipalName | foreach {Set-ADUser -Identity $_.name -UserPrincipalName ($_.SamAccountName+"@contoso.com")}
查詢結果
PS C:\Users\Administrator> Get-ADUser -Filter * -Properties * | where {$_.UserPrincipalName -eq $null} | Select-Object name,SamAccountName,UserPrincipalName name SamAccountName UserPrincipalName ---- -------------- ----------------- Guest Guest krbtgt krbtgt mailuser2 mailuser2 mailuser3 mailuser3 mailuser4 mailuser4 mailuser5 mailuser5 mailuser6 mailuser6 mailuser7 mailuser7 mailuser8 mailuser8 
設置結果
Get-ADUser -Filter * -Properties * | where {$_.UserPrincipalName -ne $null} | Select-Object name,SamAccountName,UserPrincipalName name SamAccountName UserPrincipalName ---- -------------- ----------------- Administrator Administrator Administrator@demo.com Guest Guest Guest@demo.com krbtgt krbtgt krbtgt@demo.com Exchange Online-ApplicationAccount $331000-K0SAH4NCDJ2K 

          

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM