简体   繁体   中英

Connect Office 365 Exchange Online through Powershell

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM