社内SEの覚え書き

WindowsServer、SQLServerなどなど、MS系技術について。時々日常について

Office365へのPowerShellによる接続

毎度、毎度、認証情報をポチポチ入れるのがあほらしくなったので、

$adminUser1 = "administrator@hogehoge.com"
$adminPass1 = "123"
function o365ps {
$Officepass = ConvertTo-SecureString $adminPass1 -AsPlainText -Force
$Global:Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $adminUser1,$Officepass
$URL = 'https://ps.outlook.com/powershell/'
$Session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session1
Import-Module MsOnline
Connect-MsolService -Credential $Cred
}

①↑をprofile.ps1とかにして、C:\Users\ユーザー名\Documents\WindowsPowerShell配下に置く。
PowerShell起動する。
 Set-ExecutionPolicyはやっときましょう
③o365psと入力してEnter

素敵になりました。それにしても、ウチの会社、MS製品ばっかりだなと改めて思う。