How to manage Office365 with Powershell, Exchange

namerg
namerg used Ask the Experts™
on
Hello, i am just wondering how to use powershell commandlets to manage office365 Exchange ?

I am able to create the session and remove the session but i cannot query anything like querying the mailboxes...

Thanks for your help,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
timgreen7077Exchange Engineer
Distinguished Expert 2018

Commented:
please share the cmdlets you are using to create the session, because if you are connecting correctly to the session and you have the correct permissions in O365 you should be able to search mailboxes just as you would on-prem. send all the cmdlets you are using to connect to exchange online so that we can see if you are using the correct ones to connect.
FOXActive Directory/Exchange Engineer
Top Expert 2015

Commented:
namerg,
Copy the below and paste it in notepad.  Save it to a folder of your liking Example c:\temp.   Name the text file o365connect.ps1  
Open powershell
Now change directories to c:\temp.  Once there type this     .\o365connect.ps1 and press Enter   You will be prompted for your o365 admin credentials, once you log in you will be able to perform your tasks.

$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

namergSystems Administrator

Author

Commented:
The is what i have done so far, i am able to connect and get data through CLI i will be able to insert the code within Solarwinds.

$UserCredential = Get-Credential
#Here i enter the following account SolarWinds-O365@domain.com
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

#I am stuck here

Get-PSSession | Remove-PSSession

Open in new window


I will try Fox's comment.
namergSystems Administrator

Author

Commented:
Hmm, Fox what you posted is just session/connection creation. It does not take anywhere...
namergSystems Administrator

Author

Commented:
And i have done what Jeremy posted...it is just session creation....
Senior Network Consultant / Engineer
Commented:
You're almost there. Try this:

$UserCredential = Get-Credential
#Here i enter the following account SolarWinds-O365@domain.com
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking

Open in new window

I changed the New-PSSession to what MS recommends and added the Import-PSSession. After that you should be able to run the Exchange cmdlets.
namergSystems Administrator

Author

Commented:
Hmm, this line is not right...

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection$UserCredential -Authentication Basic -AllowRedirection

Open in new window

Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
Yeah, I noticed that it pasted funny. I edited it and it should be right now.

When you import the session you should see something like this:
ModuleType Version    Name                                ExportedCommands                                                                                                                                
---------- -------    ----                                ----------------                                                                                                                                
Script     1.0        tmp_mvnb1deq.rdx                    {Add-AvailabilityAddressSpace, Add-DistributionGroupMember, Add-MailboxFolderPermission, Add-MailboxLocation...}

Open in new window

You can then run the following to see what commands are available:
gcm -Module tmp_*

Open in new window

You should see all the Exchange cmdlets your user has access to.
namergSystems Administrator

Author

Commented:
Thank You....and I was able to use Exchange Cmdlets. I think i am all set....
Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
Glad to help. :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial