切换到宽版
  • 4391阅读
  • 1回复

[分享]屌丝版 Office365 远程powershell用户管理脚本 [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2016-06-30
  1. # 远程脚本连到 Office365 服务器 基本用户管理
  2. # Powershell script for office365 user management
  3. # By Linyiyao@hotmail.com
  4. # Download Microsoft Online Services Sign-in Assistant to sign into Office 365.
  5. # http://www.microsoft.com/en-us/download/details.aspx?id=41950
  6. # Download Azure Active Directory (AD) Module so that you can perform administrative tasks in Office 365.
  7. # http://go.microsoft.com/fwlink/p/?linkid=236297
  8. #下方预设置 管理员,收件人,批量导入时CSV文件目录,以及购买订阅名称.
  9. $365_AdminAccount="admin@domain.com"
  10. $365_ReportRecipient ="admin@domain.com"
  11. $365_domainname="@Your-domain.com"    
  12. $365_Licsname="SkuPartNumber" # Get-MsolSubscription | select SkuPartNumber
  13. $365_importfilepath="D:\OneDrive\Powershell\Office365"+“\”
  14. $365_MSOLCred = Get-Credential -Credential $365_AdminAccount
  15. Connect-MsolService -Credential $365_MSOLCred
  16. if (!$?) {  
  17.        write-host -ForegroundColor yello  " `n Login Failture! Check Credential! `n "
  18.         Get-PSSession | Remove-PSSession
  19.         Get-Variable -Name 365* | Remove-Variable
  20.         return
  21.         }
  22. function AddMSOLUser {
  23.           
  24.             $365_Fname=read-host 'Input User First-name'
  25.             $365_Lname=Read-Host 'Input User Last-name'
  26.           
  27.                 
  28.           if( $365_Fname -cmatch "[^a-z0A-Z1-9]" -or  $365_Lname -cmatch "[^a-z0A-Z1-9]")
  29.           {
  30.           write-host -ForegroundColor Red “ `n Include Special Characters,PLease Input Again `n ”
  31.           continue
  32.           AddMSOLUser
  33.           }
  34.           
  35.                   
  36.           elseif($365_Lname -eq '')
  37.            {$365_Username=$365_Fname+$365_domainname
  38.            $365_DisplayName=$365_Fname}
  39.           
  40.           
  41.           elseif( $365_Fname -eq '')
  42.           {$365_Username=$365_Lname+$365_domainname
  43.            $365_DisplayName=$365_Lname}
  44.           
  45.         
  46.           else
  47.           {$365_Username=$365_Fname+"."+$365_Lname+$365_domainname
  48.            $365_DisplayName=$365_Fname+" "+$365_Lname
  49.            }                
  50.         
  51.         $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
  52.         
  53.         if (!$?){ continue
  54.           AddMSOLUser }  
  55.               
  56.         else
  57.         {
  58.         $365_body="<strong>User Name:</strong>"+ $365_Username+"<BR>"
  59.         $365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"
  60.         Write-host -ForegroundColor Green " `n A user account $365_Username has been created  `n "
  61.           
  62.         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
  63.         
  64.         Write-host -ForegroundColor Green " `n------------Done!!!------------ `n "
  65.         }
  66.         }
  67. function ResetMSOLUser {              
  68.         
  69.         $365_LeftUserName=Read-host '=====Input UserName =====>'
  70.          if( $365_LeftUserName -cmatch "[^a-z@.0A-Z1-9]")
  71.           {
  72.           write-host -ForegroundColor red  “ `n Include Special Characters,PLease Input Again `n ”
  73.           continue
  74.           ResetMSOLUser
  75.           }
  76.         elseif($365_LeftUserName.Contains('@'))
  77.         {$365_Username=$365_LeftUserName}
  78.         else{$365_username=$365_LeftUserName+$365_domainname}
  79.         
  80.         $365_temp_pwd=Set-MsolUserPassword -UserPrincipalName $365_Username  -ForceChangePassword $true
  81.         
  82.          if (!$?){ continue
  83.          ResetMSOLUser }
  84.         
  85.         else{ write-host -ForegroundColor Green “ `n A user password has been reset `n "
  86.         $365_body="<strong>User Name:</strong>"+ $365_Username+"<BR>"
  87.         $365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd+"<BR>"
  88.         $365_body+="Go to the sign-in page,https://portal.office.com ,Please Change Temporary Password ASAP!"+"<BR>"
  89.                 
  90.         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
  91.         
  92.         write-host -ForegroundColor Green " `n------------Done!!!------------ `n "}
  93.         
  94.         }
  95. function SetResigned {
  96.         $365_LeftUserName=Read-host '=====Input Left-User Name =====>'
  97.         if ($365_LeftUserName -cmatch "[^a-z@.A-Z]")
  98.         { write-host -ForegroundColor Yellow  “ `n Contains special characters, please re-enter!!!  `n ”
  99.           SetResigned
  100.           }
  101.          if($365_LeftUserName.Contains('@') ){$365_Username=$365_LeftUserName}
  102.          else{$365_username=$365_LeftUserName+$365_domainname}
  103.         $365_RedirRecer=Read-host '=====Input ReDire Address =====>'
  104.         
  105.         if ($365_RedirRecer -cmatch "[^a-z@.A-Z]")
  106.         { write-host -ForegroundColor Yellow  “ `n Contains special characters, please re-enter!!!  `n ”
  107.           SetResigned
  108.           }
  109.         if($365_RedirRecer.Contains('@')) {$365_RedirAddress=$365_RedirRecer}
  110.         else{$365_RedirAddress=$365_RedirRecer+$365_domainname}
  111.         
  112.         
  113.         $365_temp_pwd=Set-MsolUserPassword -UserPrincipalName $365_Username
  114.         write-host -ForegroundColor Yellow "`n$365_username's password has been reset!"
  115.         $365_userinfo=Get-MsolUser -UserPrincipalName $365_username
  116.         $365_NewDisplayname="Resigned "+(get-date).ToString('yyyy-MM-dd')+" "+$365_userinfo.firstname+" "+$365_userinfo.lastname
  117.         Set-MsolUser -UserPrincipalName $365_Username -DisplayName  $365_NewDisplayname
  118.         write-host -ForegroundColor Yellow "$365_username's displayname has been reset!"
  119.         $365_body="<strong>User Name:</strong>"+ $365_Username+"<BR>"
  120.         $365_body+="<strong>Temporary Password:</strong>"+ $365_temp_pwd+"<BR>"
  121.         $365_body+="<strong>DisplayName:</strong>"+$365_NewDisplayname+"<BR>"
  122.         $365_body+="<strong>Please note:</strong>we only keep this account for 3 months.It will be removed 90 days later."+"<BR>"
  123.         if ($365_RedirRecer -ne '')
  124.         {
  125.         Get-PSSession | Remove-PSSession  
  126.         $365_EXOL = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $365_MSOLCred -Authentication "Basic" -AllowRedirection
  127.         Import-PSSession $365_EXOL
  128.         $365_TRDetail=New-TransportRule -Name $365_NewDisplayname -RedirectMessageTo $365_RedirAddress -SentTo $365_Username | select name,Description
  129.         $365_body+=$365_TRDetail.Description + "<BR>"
  130.         write-host -ForegroundColor Yellow " A new Transport Rule has been added "
  131.           }
  132.         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
  133.         write-host -ForegroundColor Green " `n------------All Done!!!------------ `n "
  134.         }      
  135. function ImportUsers {
  136. $365_Importfilename=Read-host '===== Input ImportFilepath =====>'
  137. $365_ImportFileFUllname=$365_importfilepath+$365_Importfilename
  138. $365_Newuserlist=Import-Csv $365_ImportFileFUllname
  139. $365_body=""
  140. ForEach( $365_newusers in $365_Newuserlist )
  141.         {  
  142.         
  143.         if ($365_newusers.LastName -eq '' )
  144.         {
  145.     $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
  146.     $365_body+="<strong>User Name:</strong>"+ ($365_newusers.FirstName + "@Your-domain.com")+"<BR>"
  147.     $365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"
  148.         }
  149.     
  150.         elseif( $365_newusers.firstName -eq '' )
  151.             {
  152.     $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
  153.     $365_body+="<strong>User Name:</strong>"+ ($365_newusers.lastName + "@Your-domain.com") +"<BR>"
  154.     $365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"
  155.             }
  156.     
  157.             else
  158.                 {
  159.     $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
  160.     $365_body+="<strong>User Name:</strong>"+ ($365_newusers.FirstName+"."+$365_newusers.lastName+"@Your-domain.com") +"<BR>"
  161.     $365_body+="<strong>Temporary Password:</strong>"+$365_temp_pwd.password+"<BR>"      
  162.                 }
  163.                 }
  164.      write-host -ForegroundColor yellow "all users have been created"
  165.      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
  166.         
  167.                      }
  168. function DeleteUser {
  169.         
  170.         write-host -ForegroundColor yellow " `n                   Caution:This Option will Remove An Account  `n "
  171.               
  172.         $365_LeftUserName=Read-host '=====Input UserName =====>'
  173.         
  174.         if( $365_LeftUserName -cmatch "[^a-z@.A-Z01-9]")
  175.               {
  176.               write-host -ForegroundColor Yellow “ `n Include Special Characters,PLease Input Again `n ”
  177.               continue
  178.               DeleteUser
  179.               }
  180.         elseif($365_LeftUserName.Contains('@'))
  181.                     {$365_Username=$365_LeftUserName}
  182.         else{$365_username=$365_LeftUserName+$365_domainname}
  183.         Remove-MsolUser -UserPrincipalName $365_Username
  184.         if (!$?){ continue
  185.         "Fail to Delete $365_Username "
  186.          ResetMSOLUser }
  187.          else
  188.          {
  189.         write-host " `n User account $365_username has been delete `n " -ForegroundColor Green
  190.          }
  191.         }
  192. function MSOLUsermgmt {
  193.     Do
  194.     {
  195.     Write-Host -ForegroundColor green     "`n                       1.AddMSOLUser|1.添加用户"
  196.     Write-Host -ForegroundColor Cyan      "                       2.ResetMSOLUser|2.重置密码"
  197.     Write-Host -ForegroundColor Yellow    "                       3.SetResigned|3.离职员工"
  198.     Write-Host -ForegroundColor White     "                       4.ImportUeser|4.批量导入"
  199.     Write-Host -ForegroundColor red       "                       5.DeleteUser|5.删除用户"
  200.     Write-Host -ForegroundColor Magenta   "                       Press Y to Quit|按Y键退出"
  201.   
  202.     $365_options=read-host 'Pls Select A Option =>'
  203.     
  204.       if    ($365_options -eq "1" )
  205.             { AddMSOLUser  }  
  206.       elseif($365_options -eq "2" )
  207.             { ResetMSOLUser}  
  208.       elseif($365_options -eq "3" )
  209.             { SetResigned  }
  210.       elseif($365_options -eq "4" )
  211.             { ImportUsers  }
  212.       elseif($365_options -eq "5" )
  213.             { DeleteUser   }
  214.       else  {" Press any key or word to continue,OR Input 'y' to Quit!!! "}
  215.   
  216.     }
  217.     until ($365_options -eq "y" )
  218. }
  219. MSOLUsermgmt




1
分享到
只看该作者 1楼  发表于: 2016-06-30
根本没法编辑了.
很多东西要解释.主要是变量有点多.
1
快速回复
限60 字节
 
上一个 下一个