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