Connect Office 365 Exchange Online through Powershell

一个人想着一个人 提交于 2020-02-05 06:11:06

问题


I am trying to perform some operations on Exchange online(Office 365) through powershell.

First I created a new powershell session and exported the modules to local as "o365", so that on any later operation I no need to use Import-PsSession to download the required modules

$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Export-PsSession -session $s -outputModule o365

Now, I am creating new session and importing the existing module "o365".

$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Import-Module o365
Get-DistributionGroup

While running the command "Get-DistributionGroup", powershell prompts me to enter the office 365 credentials once again. Is it possible to avoid entering the credentials once again? I don't want to use Import-PsSession, since it takes more time.


回答1:


It's prompting because you're asking it to each time. I would set up $cred by creating a new object.

#Initiate Get-Credential cmdlet and store inputs
$getcred = Get-Credential
$username = $getcred.UserName
$password = $getcred.Password 

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

If you place the above in its own function, you wont have the issue of re-defining the $getcred variable. (That to most is obvious, but thought I'd cover that base)



来源:https://stackoverflow.com/questions/21719212/connect-office-365-exchange-online-through-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!