这段时间需要大量地修改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. 阅读基于sketch的软件定义网络测量数据平面硬件模型

    概要 硬件实现 基于sketch 功能:采集包数.流长数据,恢复五元组 重点:高速条件下性能较好,节省硬件资源 摘要: 提出一种基于sketch 数据结构的软件定义测量数据平面硬件模型,并在以现场可编 ...

  2. java 企业门户网站 源码 自适应响应式 freemarker 静态引擎 html5 SSM

    官网 http://www.fhadmin.org/ 系统介绍: 1.网站后台采用主流的 SSM 框架 jsp JSTL,网站后台采用freemaker静态化模版引擎生成html 2.因为是生成的ht ...

  3. sqldeveloper建立新的连接是出现Status : Failure -Test failed: IO Error: The Network Adapter could not establish the connection

    Status : Failure -Test failed: IO Error: The Network Adapter could not establish the connection解决办法: ...

  4. sql中查询某月某年内的记录

    假设表结构:用户名,日期,上班时间,下班时间.8月份记录:select * from 表名 where month(日期)=8 and 用户名 = '小张'8月份迟到早退次数:select sum(i ...

  5. struct tm

    struct tm { int tm_sec; /* 秒–取值区间为[0,59] */ int tm_min; /* 分 - 取值区间为[0,59] */ int tm_hour; /* 时 - 取值 ...

  6. code#5 P3 我有矩阵,你有吗?

    我有矩阵,你有吗?   时间限制: 1.0 秒 空间限制: 128 MB 相关文件: 题目目录 题目描述 企鹅豆豆手里有两个 01 矩阵 A 和 B.他可以进行两种操作: 选择 A 矩阵的一行,然后把 ...

  7. eclipse的git插件安装、配置与使用

    Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重点在于介绍Git的基本命令和使用技巧,让你尝试使用Git的同时,体验到原来一个版 本控制工具可以对开发产生如此之多的影响,文章分为两部分, ...

  8. LVS负载均衡机制之LVS-DR模式工作原理以及简单配置

    本博文主要简单介绍一下LVS负载均衡集群的一个基本负载均衡机制:LVS-DR:如有汇总不当之处,请各位在评论中多多指出. LVS-DR原理: LVS的英文全称是Linux Virtual Server ...

  9. C# char[]与string互相转换的两种方法

    1.string转换为char[]:char[] string.ToCharArray(); static void Main(string[] args)        {            s ...

  10. Archlinux+gnome安装中文输入法

    环境:archlinux+gnome 1.首先需要配置Archlinuxcn源 打开/etc/pacman.conf,添加 [archlinuxcn] Server = https://mirrors ...