需求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小脚本--批量添加用户属性----导出登录时间的更多相关文章

  1. Shell 脚本批量添加用户和用户密码

    #!/bin/bash#批量添加用户 设置密码for i in `seq 1 10`do if ! id user$i &> /dev/null then useradd user$i ...

  2. (1)使用bash脚本实现批量添加用户

    脚本实现内容: 可以指定用户名前缀,指定添加数量的批量添加用户脚本,密码为10为随机小写字母,并把用户名和密码写入文件中. 脚本代码: #!/bin/bashread -p "用户名前缀:& ...

  3. (转)通过shell脚本实现批量添加用户和设置随机密码以及生产环境如何批量添加

    通过shell脚本实现批量添加用户和设置随机密码以及生产环境如何批量添加 原文:http://www.21yunwei.com/archives/4773 有一个朋友问我如何批量创建用户和设置密码 , ...

  4. 工程师技术(五):Shell脚本的编写及测试、重定向输出的应用、使用特殊变量、编写一个判断脚本、编写一个批量添加用户脚本

    一.Shell脚本的编写及测 目标: 本例要求两个简单的Shell脚本程序,任务目标如下: 1> 编写一个面世问候 /root/helloworld.sh 脚本,执行后显示出一段话“Hello ...

  5. centos7 lvm合并分区脚本初探-linux性能测试 -centos7修改网卡名字-jdk环境安装脚本-关键字查询文件-批量添加用户

    1.#!/bin/bash lvmdiskscan | grep centos > /root/a.txt a=`sed -n '1p' /root/a.txt` b=`sed -n '2p' ...

  6. shell编写一个批量添加用户脚本

                                                          shell编写一个批量添加用户脚本 5.1问题 本例要求在虚拟机server0上创建/roo ...

  7. shell脚本 批量添加删除用户

    2021-07-26 1.批量添加用户 # 编写脚本 vi add_student_50.sh # 添加用户组 student groupadd student # 添加用户 student1-stu ...

  8. 【转载】干货再次来袭!Linux小白最佳实践:《超容易的Linux系统管理入门书》(连载八)用命令实现批量添加用户

    Windows添加用户需要至少5个界面,而Linux一条命令就搞定了,这是不是高效人士办公第一法则呢.本文不给你一堆参数和选项,不让你见识教条主义,只给你最实用的代码. 想每天能听到小妞的语音播报,想 ...

  9. Centos6配置samba服务器并批量添加用户和文件夹

    一.需求 局域网内有若干用户,所有用户访问一个共享目录 每个用户在共享目录里有自己的文件夹 每个用户都可以读取其他人的文件夹 每个用户只能对自己的文件夹有写入权限 所有用户都属于filesgroup组 ...

随机推荐

  1. 【hibernate-validator+SpringMVC】后台参数校验框架

    hibernate-validator+SpringMVC 简介:简单说,就是对Entity进行校验. 1.导包,没有很严谨的对应关系,所以我用了比较新的版本,支持更多的注解. <depende ...

  2. react 项目全家桶构件流程

    资源:create-react-app.react.react-dom.redux.react-redux.redux-thunk.react-router-dom.antd-mobile/antd. ...

  3. Spring5深度源码分析(三)之AnnotationConfigApplicationContext启动原理分析

    代码地址:https://github.com/showkawa/spring-annotation/tree/master/src/main/java/com/brian AnnotationCon ...

  4. hive 之 Cube, Rollup介绍

    1. GROUPING SETS GROUPING SETS作为GROUP BY的子句,允许开发人员在GROUP BY语句后面指定多个统维度,可以简单理解为多条group by语句通过union al ...

  5. 跟着大彬读源码 - Redis 2 - 服务器如何响应客户端请求?(上)

    上次我们通过问题"启动服务器,程序都干了什么?",跟着源码,深入了解了 Redis 服务器的启动过程. 既然启动了 Redis 服务器,那我们就要连上 Redis 服务干些事情.这 ...

  6. 并发编程-concurrent指南-ConcurrentMap

    ConcurrentMap 是个接口,你想要使用它的话就得使用它的实现类之一. ConcurrentMap,它是一个接口,是一个能够支持并发访问的java.util.map集合: 在原有java.ut ...

  7. 常用的URL Scheme

    系统 短信 sms:// app store itms-apps:// 电话 tel:// 备忘录 mobilenotes:// 设置 prefs:root=SETTING E-Mail MESSAG ...

  8. C++中 =default,=delete用法

    =default: 用于显式要求编译器提供合成版本的四大函数(构造.拷贝.析构.赋值) 例如: class A{ public: A() = default; A(const A& a) = ...

  9. S7-300CPU存储器介绍及存储卡使用

    1. S7 300存储区概述 S7-300 PLC的存储区可以划分为四个区域:装载存储器(Load Memory).工作存储器(Work Memory). 系统存储器(System Memory)和保 ...

  10. dapper支持DataSet

    在源代码中添加 /// <summary> /// describe:支持 DataSet /// </summary> /// <param name="cn ...