当前位置: 首页 > 科技观察

如何安全连接Office365Online

时间:2023-03-14 17:00:33 科技观察

随着Office365在中国的快速普及,越来越多的企业开始使用Office365及相关服务。能够熟练使用和管理Office365已经成为大多数企业IT管理员的必备技能。今天我们就来介绍一种连接Office365Online相对安全便捷的方式,即通过在PowerShell界面对用户名和密码进行加密连接Office365Online。那么我们使用PowerShell来远程管理Office365Online,它有以下优点:Office365拥有只有使用Office365PowerShell才能配置的功能Office365PowerShell擅长批量操作Office365PowerShell擅长过滤数据Office365PowerShell方便打印或保存数据Office365PowerShell支持跨服务器产品管理Office365PowerShell显示通过Microsoft365管理中心无法看到的附加信息在连接过程中,如果以明文形式输入用户名和密码,它会显示一个安全风险。如果使用下面的PowerShell脚本可以避免这个缺点:预先定义两个用于加密和解密字符串的函数;然后检查本地是否有加密的用户名和密码文件,如果没有,提示用户输入用户名和密码,并以密文保存在本地;最后,读取并解密本地加密的用户名和密码,用于远程连接Office365Online。脚本代码分为以下三部分给大家介绍。第一部分定义了加密和解密函数。#Thisfunctionistoencryptastring.functionEncrypt-String($String,$Passphrase,$salt="SaltCrypto",$init="IV_Password",[switch]$arrayOutput){$r=new-ObjectSystem.Security.Cryptography.RijndaelManaged$pass=[Text.Encoding]::UTF8.GetBytes($Passphrase)$salt=[Text.Encoding]::UTF8.GetBytes($salt)$r.Key=(new-Object`Security.Cryptography.PasswordDeriveBytes$pass,$salt"SHA1",5).GetBytes(32)$r.IV=(new-Object`Security.Cryptography.SHA1Managed).ComputeHash`[Text.Encoding]::UTF8.GetBytes($init))[0..15]$c=$r.CreateEncryptor()$ms=new-ObjectIO.MemoryStream$cs=new-ObjectSecurity.Cryptography.CryptoStream$ms,$c,"写入"$sw=new-ObjectIO.StreamWriter$cs$sw.Write($String)$sw.Close()$cs.Close()$ms.Close()$r.Clear()[byte[]]$result=$ms.ToArray()return[Convert]::ToBase64String($result)}#Thisfunctionistode-encryptastring.functionDecrypt-String($Encrypted,$Passphrase,$salt="SaltCrypto",$init="IV_Password"){如果($Encrypted-is[string]){$Encrypted=[Convert]::FromBase64String($Encrypted)}$r=new-ObjectSystem.Security.Cryptography.RijndaelManaged$pass=[Text.Encoding]::UTF8.GetBytes($密码)$salt=[文本编码]::UTF8.GetBytes($salt)$r.Key=(new-ObjectSecurity.Cryptography.PasswordDeriveBytes`$pass,$salt,"SHA1",5).GetBytes(32)$r.IV=(new-Object`Security.Cryptography.SHA1Managed).ComputeHash`([Text.Encoding]::UTF8.GetBytes($init))[0..15]$d=$r.CreateDecryptor()$ms=new-ObjectIO.MemoryStream@(,$Encrypted)$cs=new-ObjectSecurity.Cryptography.CryptoStream$ms,$d,"读取"$sr=new-ObjectIO.StreamReader$csWrite-Output$sr.ReadToEnd()$sr.Close()$cs.Close()$ms.Close()$r.Clear()}Clear-Host第二部分,只从本地文本文件第一次读取加密后的Office365用户名和密码你需要手动输入用户名和密码,然后将加密后的用户名和密码以密文形式存储到本地磁盘。之后不需要输入。#Trytoreadtheencryptedusernameandpasswordfromthespecificpath,ifthere,readandde-encryptthem.Iftherearenot,提示输入和加密。$Home\Desktop\password.txt'If($null-ne$uencrypted-and$null-ne$pencrypted){$udecrypted=解密字符串$uencrypted"U_MyStrongPassword"$pdecrypted=解密字符串$pencrypted"P_MyStrongPassword"$pdecrypted=ConvertTo-SecureString$pdecrypted-AsPlainText-Force}Else{$ustring=read-host"PleaseEnterOffice365Username"$pstring=read-host"PleaseEnterOffice365UserPassword"$uencrypted=加密字符串$ustring"U_MyStrongPassword"$uencrypted|Out-File"$HOME\Desktop\Username.txt"write-host"StoretheencryptedUsernamesuccessfully!"$pencrypted=Encrypt-String$pstring"P_MyStrongPassword"$pencrypted|Out-File"$HOME\Desktop\password.txt"write-host"成功存储加密密码!"$udecrypted=解密字符串$uencrypted"U_MyStrongPassword"$pdecrypted=解密字符串$pencrypted"P_MyStrongPassword"$pdecrypted=ConvertTo-SecureString$pdecrypted-Force-As}纯文本部分,连接Office365Online执行以下命令后,就可以在PowerShell下远程管理Office365ExchangeOnline#ConnecttoOffice365onlineorAzure$LiveCred=New-ObjectSystem.Management.Automation.PSCredential$udecrypted,$pdecrypted$Session=New-PSSession-ConfigurationNameMicrosoft.Exchange`-ConnectionUrihttps://partner.outlook.cn/powershell-Credential$LiveCred`-AuthenticationBasic–AllowRedirection-ErrorActionStop`-Name"$($Credential.UserName)"Import-PSSession$SessionConnect-MsolService–Credential$LiveCred-AzureEnvironmentAzureChinaCloud注意:执行最后一条命令需要预装MicrosoftOnlineServicesSign-InAssistant,安装方法百度即可,本文不再介绍本文。