码迷,mamicode.com
首页 > 系统相关 > 详细

powershell小脚本--批量添加用户属性----导出登录时间

时间:2018-05-17 19:57:52      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:reac   obj   system   power   pre   style   director   dem   number   

需求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                          

          

 

powershell小脚本--批量添加用户属性----导出登录时间

标签:reac   obj   system   power   pre   style   director   dem   number   

原文地址:https://www.cnblogs.com/liuchaogege/p/9052684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!