查看完整版本: [-- 屌丝版 Office365 远程powershell用户管理脚本 --]

Exchange技术论坛 -> Office 365 定制版用户讨论区 -> 屌丝版 Office365 远程powershell用户管理脚本 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

gtese 2016-06-30 16:01

屌丝版 Office365 远程powershell用户管理脚本

  1. # 远程脚本连到 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





gtese 2016-06-30 16:34
根本没法编辑了.
很多东西要解释.主要是变量有点多.


查看完整版本: [-- 屌丝版 Office365 远程powershell用户管理脚本 --] [-- top --]



Powered by phpwind v8.7.1 Code ©2003-2011 phpwind
Time 0.023038 second(s),query:6 Gzip enabled