切换到宽版
Exchange 中文站
Exchange 2003
Exchange 2007
Exchange 2010
Exchange 2013
勋章中心
下拉
用户名
电子邮箱
用户名
密 码
记住登录
登录
找回密码
注册
快捷通道
关闭
您还没有登录,快捷通道只有在登录后才能使用。
立即登录
还没有帐号? 赶紧
注册一个
Exchange论坛
Exchange视频
Exchange技术论坛
>
Office 365 定制版用户讨论区
>
屌丝版 Office365 远程powershell用户管理脚本
发帖
回复
返回列表
5151
阅读
1
回复
[分享]
屌丝版 Office365 远程powershell用户管理脚本
[复制链接]
上一主题
下一主题
gtese
UID:22736
注册时间
2014-07-16
最后登录
2016-08-08
在线时间
24小时
发帖
8
搜Ta的帖子
精华
0
金币
23
访问TA的空间
加好友
用道具
技术学徒
关闭
个人中心可以申请新版勋章哦
立即申请
知道了
加关注
发消息
只看楼主
倒序阅读
0楼
发表于: 2016-06-30
复制代码
# 远程脚本连到 Office365 服务器 基本用户管理
# Powershell script for office365 user management
# By Linyiyao@hotmail.com
# Download Microsoft Online Services Sign-in Assistant to sign into Office 365.
# http://www.microsoft.com/en-us/download/details.aspx?id=41950
# Download Azure Active Directory (AD) Module so that you can perform administrative tasks in Office 365.
# http://go.microsoft.com/fwlink/p/?linkid=236297
#下方预设置 管理员,收件人,批量导入时CSV文件目录,以及购买订阅名称.
$365_AdminAccount="admin@domain.com"
$365_ReportRecipient ="admin@domain.com"
$365_domainname="@Your-domain.com"
$365_Licsname="SkuPartNumber" # Get-MsolSubscription | select SkuPartNumber
$365_importfilepath="D:\OneDrive\Powershell\Office365"+“\”
$365_MSOLCred = Get-Credential -Credential $365_AdminAccount
Connect-MsolService -Credential $365_MSOLCred
if (!$?) {
write-host -ForegroundColor yello " `n Login Failture! Check Credential! `n "
Get-PSSession | Remove-PSSession
Get-Variable -Name 365* | Remove-Variable
return
}
function AddMSOLUser {
$365_Fname=read-host 'Input User First-name'
$365_Lname=Read-Host 'Input User Last-name'
if( $365_Fname -cmatch "[^a-z0A-Z1-9]" -or $365_Lname -cmatch "[^a-z0A-Z1-9]")
{
write-host -ForegroundColor Red “ `n Include Special Characters,PLease Input Again `n ”
continue
AddMSOLUser
}
elseif($365_Lname -eq '')
{$365_Username=$365_Fname+$365_domainname
$365_DisplayName=$365_Fname}
elseif( $365_Fname -eq '')
{$365_Username=$365_Lname+$365_domainname
$365_DisplayName=$365_Lname}
else
{$365_Username=$365_Fname+"."+$365_Lname+$365_domainname
$365_DisplayName=$365_Fname+" "+$365_Lname
}
$365_temp_pwd=New-MsolUser -FirstName $365_Fname -LastName $365_Lname -UserPrincipalName $365_Username -DisplayName $365_DisplayName -LicenseAssignment $365_Licsname -UsageLocation US -ForceChangePassword $true -PasswordNeverExpires $true |select password
if (!$?){ continue
AddMSOLUser }
else
{
$365_body="<strong>User Name:</strong>"+ $365_Username+"<BR>"
$365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"
Write-host -ForegroundColor Green " `n A user account $365_Username has been created `n "
Send-MailMessage -From $365_AdminAccount -Subject "User account $365_Username has been created or modified" -To $365_ReportRecipient -body $365_body -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -Port 587 -SmtpServer $365_smtpserver -UseSsl -Credential $365_MSOLCred
Write-host -ForegroundColor Green " `n------------Done!!!------------ `n "
}
}
function ResetMSOLUser {
$365_LeftUserName=Read-host '=====Input UserName =====>'
if( $365_LeftUserName -cmatch "[^a-z@.0A-Z1-9]")
{
write-host -ForegroundColor red “ `n Include Special Characters,PLease Input Again `n ”
continue
ResetMSOLUser
}
elseif($365_LeftUserName.Contains('@'))
{$365_Username=$365_LeftUserName}
else{$365_username=$365_LeftUserName+$365_domainname}
$365_temp_pwd=Set-MsolUserPassword -UserPrincipalName $365_Username -ForceChangePassword $true
if (!$?){ continue
ResetMSOLUser }
else{ write-host -ForegroundColor Green “ `n A user password has been reset `n "
$365_body="<strong>User Name:</strong>"+ $365_Username+"<BR>"
$365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd+"<BR>"
$365_body+="Go to the sign-in page,https://portal.office.com ,Please Change Temporary Password ASAP!"+"<BR>"
Send-MailMessage -From $365_AdminAccount -Subject "User account $365_Username Password has been reset" -To $365_ReportRecipient -body $365_body -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -Port 587 -SmtpServer $365_smtpserver -UseSsl -Credential $365_MSOLCred
write-host -ForegroundColor Green " `n------------Done!!!------------ `n "}
}
function SetResigned {
$365_LeftUserName=Read-host '=====Input Left-User Name =====>'
if ($365_LeftUserName -cmatch "[^a-z@.A-Z]")
{ write-host -ForegroundColor Yellow “ `n Contains special characters, please re-enter!!! `n ”
SetResigned
}
if($365_LeftUserName.Contains('@') ){$365_Username=$365_LeftUserName}
else{$365_username=$365_LeftUserName+$365_domainname}
$365_RedirRecer=Read-host '=====Input ReDire Address =====>'
if ($365_RedirRecer -cmatch "[^a-z@.A-Z]")
{ write-host -ForegroundColor Yellow “ `n Contains special characters, please re-enter!!! `n ”
SetResigned
}
if($365_RedirRecer.Contains('@')) {$365_RedirAddress=$365_RedirRecer}
else{$365_RedirAddress=$365_RedirRecer+$365_domainname}
$365_temp_pwd=Set-MsolUserPassword -UserPrincipalName $365_Username
write-host -ForegroundColor Yellow "`n$365_username's password has been reset!"
$365_userinfo=Get-MsolUser -UserPrincipalName $365_username
$365_NewDisplayname="Resigned "+(get-date).ToString('yyyy-MM-dd')+" "+$365_userinfo.firstname+" "+$365_userinfo.lastname
Set-MsolUser -UserPrincipalName $365_Username -DisplayName $365_NewDisplayname
write-host -ForegroundColor Yellow "$365_username's displayname has been reset!"
$365_body="<strong>User Name:</strong>"+ $365_Username+"<BR>"
$365_body+="<strong>Temporary Password:</strong>"+ $365_temp_pwd+"<BR>"
$365_body+="<strong>DisplayName:</strong>"+$365_NewDisplayname+"<BR>"
$365_body+="<strong>Please note:</strong>we only keep this account for 3 months.It will be removed 90 days later."+"<BR>"
if ($365_RedirRecer -ne '')
{
Get-PSSession | Remove-PSSession
$365_EXOL = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $365_MSOLCred -Authentication "Basic" -AllowRedirection
Import-PSSession $365_EXOL
$365_TRDetail=New-TransportRule -Name $365_NewDisplayname -RedirectMessageTo $365_RedirAddress -SentTo $365_Username | select name,Description
$365_body+=$365_TRDetail.Description + "<BR>"
write-host -ForegroundColor Yellow " A new Transport Rule has been added "
}
Send-MailMessage -From $365_AdminAccount -Subject "User account $365_Username has been created or modified" -To $365_ReportRecipient -body $365_body -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -Port 587 -SmtpServer $365_smtpserver -UseSsl -Credential $365_MSOLCred
write-host -ForegroundColor Green " `n------------All Done!!!------------ `n "
}
function ImportUsers {
$365_Importfilename=Read-host '===== Input ImportFilepath =====>'
$365_ImportFileFUllname=$365_importfilepath+$365_Importfilename
$365_Newuserlist=Import-Csv $365_ImportFileFUllname
$365_body=""
ForEach( $365_newusers in $365_Newuserlist )
{
if ($365_newusers.LastName -eq '' )
{
$365_temp_pwd=New-MsolUser -FirstName $365_newusers.FirstName -UserPrincipalName ($365_newusers.FirstName + "@Your-domain.com") -DisplayName $365_newusers.FirstName -LicenseAssignment $365_Licsname -UsageLocation US -ForceChangePassword $true -PasswordNeverExpires $true |select password
$365_body+="<strong>User Name:</strong>"+ ($365_newusers.FirstName + "@Your-domain.com")+"<BR>"
$365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"
}
elseif( $365_newusers.firstName -eq '' )
{
$365_temp_pwd=New-MsolUser -LastName $365_newusers.lastName -UserPrincipalName ($365_newusers.lastName + "@Your-domain.com") -DisplayName $365_newusers.lastName -LicenseAssignment $365_Licsname -UsageLocation US -ForceChangePassword $true -PasswordNeverExpires $true |select password
$365_body+="<strong>User Name:</strong>"+ ($365_newusers.lastName + "@Your-domain.com") +"<BR>"
$365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"
}
else
{
$365_temp_pwd=New-MsolUser -FirstName $365_newusers.FirstName -LastName $365_newusers.lastName -UserPrincipalName ($365_newusers.FirstName+"."+$365_newusers.lastName+"@Your-domain.com") -DisplayName ($365_newusers.FirstName + " " + $365_newusers.lastName) -LicenseAssignment $365_Licsname -UsageLocation US -ForceChangePassword $true -PasswordNeverExpires $true |select password
$365_body+="<strong>User Name:</strong>"+ ($365_newusers.FirstName+"."+$365_newusers.lastName+"@Your-domain.com") +"<BR>"
$365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"
}
}
write-host -ForegroundColor yellow "all users have been created"
Send-MailMessage -From $365_AdminAccount -Subject "Users have been created" -To $365_ReportRecipient -body $365_body -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -Port 587 -SmtpServer $365_smtpserver -UseSsl -Credential $365_MSOLCred
}
function DeleteUser {
write-host -ForegroundColor yellow " `n Caution:This Option will Remove An Account `n "
$365_LeftUserName=Read-host '=====Input UserName =====>'
if( $365_LeftUserName -cmatch "[^a-z@.A-Z01-9]")
{
write-host -ForegroundColor Yellow “ `n Include Special Characters,PLease Input Again `n ”
continue
DeleteUser
}
elseif($365_LeftUserName.Contains('@'))
{$365_Username=$365_LeftUserName}
else{$365_username=$365_LeftUserName+$365_domainname}
Remove-MsolUser -UserPrincipalName $365_Username
if (!$?){ continue
"Fail to Delete $365_Username "
ResetMSOLUser }
else
{
write-host " `n User account $365_username has been delete `n " -ForegroundColor Green
}
}
function MSOLUsermgmt {
Do
{
Write-Host -ForegroundColor green "`n 1.AddMSOLUser|1.添加用户"
Write-Host -ForegroundColor Cyan " 2.ResetMSOLUser|2.重置密码"
Write-Host -ForegroundColor Yellow " 3.SetResigned|3.离职员工"
Write-Host -ForegroundColor White " 4.ImportUeser|4.批量导入"
Write-Host -ForegroundColor red " 5.DeleteUser|5.删除用户"
Write-Host -ForegroundColor Magenta " Press Y to Quit|按Y键退出"
$365_options=read-host 'Pls Select A Option =>'
if ($365_options -eq "1" )
{ AddMSOLUser }
elseif($365_options -eq "2" )
{ ResetMSOLUser}
elseif($365_options -eq "3" )
{ SetResigned }
elseif($365_options -eq "4" )
{ ImportUsers }
elseif($365_options -eq "5" )
{ DeleteUser }
else {" Press any key or word to continue,OR Input 'y' to Quit!!! "}
}
until ($365_options -eq "y" )
}
MSOLUsermgmt
共
条评分
1
回复
举报
分享到
gtese
UID:22736
注册时间
2014-07-16
最后登录
2016-08-08
在线时间
24小时
发帖
8
搜Ta的帖子
精华
0
金币
23
访问TA的空间
加好友
用道具
技术学徒
加关注
发消息
只看该作者
1楼
发表于: 2016-06-30
根本没法编辑了.
很多东西要解释.主要是变量有点多.
共
条评分
1
回复
举报
发帖
回复
返回列表
http://bbs.exchangecn.com
访问内容超出本站范围,不能确定是否安全
继续访问
取消访问
快速回复
限60 字节
您目前还是游客,请
登录
或
注册
进入高级模式
文字颜色
发 布
回复后跳转到最后一页
上一个
下一个
关闭
补充发布信息
验证码:
验证问题:
84 + 14 = ?
发 布
隐藏
快速跳转
Microsoft 企业云服务
Office 365 定制版用户讨论区
Exchange 技术讨论
Exchange Server 2016
Exchange Server 2013
Exchange Server 2010
Exchange Server 2007
Exchange Server 2003
Exchange 相关技术
Exchange 事件日志
Exchange 二次开发
Microsoft Outlook
Exchange 资源下载
Exchange 2003 资源下载
Exchange 2007 资源下载
Exchange 2010 资源下载
Exchange 招聘求职
Exchange 招聘 | 求职
Exchange 社区站务
站内站务
关闭
关闭
选中
1
篇
全选