这段时间需要大量地修改AD用户的一些属性,例如邮件,UPN,登录名等等,以便和Office365的登录账号保持一致。写了个简单脚本进行批量修改。

#Import AD Module
Import-Module ActiveDirectory
#Import Office 365 Module
$Sessions=Get-PSSession
if ($Sessions.ComputerName -like "outlook.office365.com"){
write-host "Detecting current Office365 session, skip.." -ForegroundColor Cyan
}
else{

write-host "Starting new Office365 session" -ForegroundColor Cyan
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
}
#Get Primary SMTP Address
function Get-PrimarySMTP(){
[CmdletBinding()]

Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]
$users
)
$pp=$null
$pp=@{'name'=$null;'primarysmtp'=$null}
$obj=New-Object -TypeName psobject -Property $pp

$result=@()
foreach($user in $users){
$info=get-aduser -Filter {name -eq $user} -Properties proxyaddresses
$primarySMTPAddress = ""

foreach ($address in $info.proxyAddresses)
{
if (($address.Length -gt 5) -and ($address.SubString(0,5) -ceq 'SMTP:') )
{
$primarySMTPAddress = $address.SubString(5)

break
}
}
$objtemp=$obj | select *
$objtemp.name=$info.Name
$objtemp.primarysmtp=$primarySMTPAddress
$result+=$objtemp
}
return $result
}
#Get AD User Informtion
#$ADUsers = Get-ADUser -SearchBase "ou=mango,ou=ddb_group,ou=melbourne,dc=omnicom,dc=com,dc=au" -Properties proxyaddresses, emailaddress, displayname -Filter *
Write-Host " "
$uName=Read-Host "Please input User AD name"
$ADUsers=get-aduser $uName -Properties proxyaddresses, emailaddress, displayname
#Change SamAccountName and UPN
foreach ($ADUser in $ADUsers) {
$ADUser.Name
$GivenName = $ADUser.GivenName
$SurName = $ADUser.Surname
if (($GivenName -ne $null) -or ($SurName -ne $null))
{
$newSAM = $GivenName.ToLower() + '.'+$SurName.ToLower()
$oldUPN=$ADUser.UserPrincipalName
$domainName= $oldUPN.Split('@')[1]
$newUPN = $newSAM + '@'+$domainName

write-host "Updating ADUPN: $oldupn -> $newUPN" -ForegroundColor Cyan

#Change AD UPN and SamAccount
Set-ADUser $ADUser -SamAccountName $newSAM -UserPrincipalName $newUPN

#Change AD email
$oldEmail=$ADUser.emailaddress
$newEmail=$newSAM+‘@'+$oldemail.split('@')[1]
write-host "Updating Email:$oldEmail -> $newEmail" -ForegroundColor Cyan
set-aduser $newSAM -EmailAddress $newEmail
#Change Primary SMTP
$primary=Get-PrimarySMTP -users $ADUser.name | select -ExpandProperty primarysmtp
Write-Host "Updating ProxyAddress.." -ForegroundColor Cyan
#Write-Host "Current Primary address is $primary" -ForegroundColor Cyan

$Aduser.proxyaddresses.remove("SMTP:"+$primary)

$Aduser.proxyaddresses.add("smtp:"+$primary)
$Aduser.proxyaddresses.add("SMTP:"+$newEmail)
set-aduser $newSAM -replace @{proxyaddresses=[string[]]$ADUser.proxyaddresses}

#Change cloud UPN. If Office365 session is not connected properly, follow commands wont' work!
$oldmsolupn=Get-MsolUser -SearchString $ADUser.Name
$oldmsolupn=$oldmsolupn| select -First 1 | select -ExpandProperty UserPrincipalName
$newmsolupn=$newSAM+'@'+$oldmsolupn.split('@')[1]
write-host "Updating MSOLUPN: $oldmsolupn -> $newmsolupn" -ForegroundColor Cyan
Set-MsolUserPrincipalName -UserPrincipalName $oldmsolupn -NewUserPrincipalName $newmsolupn
Write-Host ""
}
else{
Write-Warning "Either GivenName or Surname is Empty"

}
}
#Confirm result
Write-Host "Confirm AD Result " -ForegroundColor Cyan
get-aduser $newSAM -Properties proxyaddresses,mail | select Name, SamAccountName, UserPrincipalName, proxyaddresses, mail
Write-Host "Confirm O365 Result" -ForegroundColor Cyan
Get-MsolUser -SearchString $ADUser.Name | select UserPrincipalName

修改其实都满简单地,我的脚本里面也没有写太多容错处理。

如何运用 Powershell 修改Office365和AD账户的更多相关文章

  1. SharePoint自动化部署,利用PowerShell 导入用户至AD——PART II

    这是对上一篇文章<SharePoint自动化部署,利用PowerShell 导出/导入AD中的用户>进行补充.开发时,为了测试和演示,我们往往需要经常性的把用户添加到AD中.数据量小的时候 ...

  2. 配置联想IMM使用AD账户登录

    IMM是联想(IBM)服务器的管理卡Integrated Management Module的缩写,现在是第二个版本.通过它可以远程管理服务器,就像你在服务器面前操作一样.可以修改BIOS设置,可以重 ...

  3. (原创)AD账户误删导致Exchange邮箱被删 莫苦恼

    由于人员变动,离职人员AD账户和邮箱经常要删除.但是在删除AD账户的时候难免会犯错,将在用的用户给删除了,这是个痛苦的事情, 然后你会发现Exchange邮箱也会跟着删除,抓狂了..,还好,幸亏这里进 ...

  4. 通过windows powershell 修改 Office 365默认的 35MB 的邮件大小限制

    附件下载: 通过windows powershell 修改 Office 365默认的 35MB 的邮件大小限制

  5. AD账户锁定策略

    AD账户锁定策略在一个域中可以有多套,密码策略只能有一套

  6. Windows Server 2016分层式存储,使用PowerShell修改底层介质类型

    新部署的备份服务器,需要做分层式存储,按照网上最常见一个作者叫刘兵的文档,名叫<Windows Server2016分层存储技术详细拆解手册>,做到使用PowerShell修改磁盘的Med ...

  7. 如何修改MyEclipse的SVN账户和密码

    如何修改MyEclipse的SVN账户和密码呢? 操作方法:删除C:\Users\Administrator\AppData\Roaming\Subversion\auth\svn.simple文件夹 ...

  8. 使用ADMT和PES实现window AD账户跨域迁移-介绍篇

    使用 ADMT 和 pwdmig 实现 window AD 账户跨域迁移系列: 介绍篇 ADMT 安装 PES 的安装 ADMT:迁移组 ADMT:迁移用户 ADMT:计算机迁移 ADMT:报告生成 ...

  9. Azure 国内版 如何用powershell修改linux系统的密码

    国内版不像国际版本那样,一个UI按钮就解决问题,国内版很多功能上线比较慢,我们只能用powershell工具进行命令行 式的更改,也当温习一下命令了,好久不用了. $vm = Get-AzureVM ...

随机推荐

  1. leaflet 中文API

    LeafLet js 官网:http://leafletjs.com/index.html LeafLet js 官网demo: http://leafletjs.com/examples.html ...

  2. PAT——1060. 爱丁顿数

    英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数”E,即满足有E天骑车超过E英里的最大整数E.据说爱丁顿自己的E等于87. 现给定某人N天的骑车距离,请你算出对应的爱 ...

  3. Loj_6282. 数列分块入门 6

    Loj_6282 这个题目涉及到了块的重构,这里使用了\(\sqrt{n}\)次插入便重构的方法 讲重复的操作提出来做了函数 #include <iostream> #include &l ...

  4. 【luogu P3959 宝藏】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3959 我只是心血来潮想学SA(考场上骗分总行吧). 这个题可以状压DP.爆搜+剪枝.有意思的还是随机化搜索( ...

  5. 【Step By Step】将Dotnet Core部署到Docker下

    一.使用.Net Core构建WebAPI并访问Docker中的Mysql数据库 这个的过程大概与我之前的文章<尝试.Net Core—使用.Net Core + Entity FrameWor ...

  6. Linux命令总结(转)

    1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件 -A 通-a,但不列出"."和".." -l 列出 ...

  7. 书单list

    2018年对我来说是个踩到狗屎的一年,不能说运气差,只能说过去所有的不良决策后果都集中到2018年爆发了,希望新的一年,都好起来.书单很短,买的确实很多,真的惭愧. 昨日世界 南渡北归 上学记 回忆录 ...

  8. log4j与logback包冲突原因及解决,不可忽视的Warning

    场景 一个简单的spring-boot程序,需要用kafka做消息队列,于是在maven中引入kafka依赖,一切看似没问题,在启动时,打印出Warning信息: SLF4J: Class path ...

  9. oracle10g学习笔记

    1.简介 1.1.sql:Structured Query Language 结构化查询语言 1.2.windows在目录路径中使用反斜线\,unix和linux使用正斜线/ 1.3.Number(a ...

  10. SQLServer禁用、启用外键约束

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ---启用or禁用指定表所有外键约束  alter table PUB_STRU  NOCHECK constrai ...