Powershell 修改Office365和AD账户
这段时间需要大量地修改AD用户的一些属性,例如邮件,UPN,登录名等等,以便和Office365的登录账号保持一致。写了个简单脚本进行批量修改。
脚本执行的前提是在本地安装了AD和Office365必要的PS模块。AD是可以远程间接调用DC的PS模块,不过实际操作发现有些小bug,所以还是直接安装在本地比较省事,速度也快。
#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 " "
$uUser=Read-Host "Please input the domain name "
try{
$ADUsers=get-aduser $uUser -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} -ErrorAction Stop
#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
}catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
write-host "AD User can not found" -ForegroundColor red
}catch [Microsoft.ActiveDirectory.Management.ADException]{
Write-Host "User vlaue can't be updated or the specified value already exists" -ForegroundColor Red
}
修改其实都满简单地,我的脚本里面也没有写太多容错处理。修改完了之后,windows用户可能存在Profile和注册表对不上号的问题,因此还需要修改一些注册表,具体操作参考 http://beanxyz.blog.51cto.com/5570417/1930788
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
自定义菜单,仿优酷菜单
效果图如下:点击主页后,上面2个圆环旋转消失与出现,点击中间圆环的中间那个菜单按钮,最外围的圆环旋转消失于出现 利用了自定义控件技术,以及图片的旋转和布局时各个控件的相对关系。 1、acitivity_main.xml的布局文件 <?xmlversion="1.0"encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.yuanlp.youkudemo.MainActivity"> <RelativeLayout android:id="@+id/le...
-
下一篇
grep/sed/awk实战
最近准备和CoreSite - Any2 California接入商建立网络BGP邻居关系。从peeringdb上找到了所有接入商的信息,但是转移信息到本地不是很方便,需要进行多次文本调整,耗时较长。 作为萌新,立马就想到近期学习的grep/sed/awk工具。于是就尝试处理数据。 1、下载页面内容 >curlhttps://www.peeringdb.com/ix/142>peering 将页面内容存入peering文件 2、删除无用信息 翻看网页,发现第一个行需要的信息是“2degrees”,最后一行是“Zscaler AS22616” >grep-n'2degrees'peering##发现第一行是807 >sed-i'1,806'dpeering##删除1-806行 >headpeering##检查 >grep-nA8'22616'peering##发现最后一行是5161 >sed-i'5162,$'dpeering##删除5161后的行 >tailpeering##检查 ▼还有一种方法: >egrep'view_tit...
相关文章
文章评论
共有0条评论来说两句吧...

微信收款码
支付宝收款码