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 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
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69976867/viewspace-2706680/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- AD-禁用賬戶命令 Disable-ADAccount & 批量啟用賬戶命令 Enable-adaccount
- powershell無法修改字符集,非修改登錄檔修改powershell的方法
- Git-如何區分使用個人賬戶和公司賬戶Git
- EOS賬戶管理
- 企業賬戶
- MySQL賬戶管理MySql
- 企業對公賬戶走賬
- PbootCMS 修改後臺登陸地址和賬號和密碼方法boot密碼
- PowerShell快速修改多個檔案的名稱
- 公司對公賬戶過賬買賣
- 對公賬戶出售
- 全套對公賬戶
- Currenxie 環球賬戶
- win10兒童賬戶怎麼建立?win10兒童賬戶管理和許可權設定Win10
- Office365 OneDrive Geo Move
- Linux運維安全經驗-賬戶和登入安全Linux運維
- 銀行卡賬戶入賬支出問題
- win10系統無法修改管理員賬戶密碼的解決方法Win10密碼
- win10怎麼建立賬戶 window10如何建立新賬戶Win10
- win10怎麼更改賬戶 win10如何更改本地賬戶Win10
- 問題賬戶需求分析
- 全新企業賬戶
- 購買對公賬戶
- 倒賣對公賬戶
- 售賣對公賬戶
- 購買對公賬戶
- 以太坊之賬戶管理
- Ubuntu 啟用 root 賬戶Ubuntu
- 博通賬戶亂碼
- centos建立賬戶指令碼CentOS指令碼
- 從500到賬戶接管
- VSCode的git賬戶重置VSCodeGit
- win10微軟線上賬戶如何與本地賬戶切換_win10 win10本地賬戶怎麼切換到微軟線上賬戶Win10微軟
- win10 電子郵件和賬戶選項裡的賬號如何刪除Win10
- 使用remix實現給合約賬戶轉賬REM
- win10 microsoft賬戶怎麼取消_win10 microsoft賬戶如何取消Win10ROS
- Win10系統下如何退出微軟賬戶改成本地賬戶Win10微軟
- Win10中啟用和關閉管理員賬戶Win10