Get the subject of a recieved email in PowerShell - powershell

So I'm checking for an approach on a problem I have.
I have an e-mail from my school (Office 365) and I wanted to print the email subject of each email that's located in my inbox with PowerShell.
I already have found the method to lay a connection
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://smtp.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
function Connect-O365 {
$session365 = New-PSSession `
-ConfigurationName Microsoft.Exchange `
-ConnectionUri "https://smtp.office365.com/powershell-liveid/" `
-Credential $UserCredential
-Authentication Basic `
-AllowRedirection
Import-Module (Import-PSSession $session365 -AllowClobber) -Global
}
And have found the Get-Mailbox cmdlet.
The problem now however is that I haven't found any real examples or methods that continue to help me printing the email subjects.
I have done quite some research and didn't manage to find something like:
Get-Mailbox -Identity "user" |Select-MailBox * |Where-Object $_.MailBoxName = "Inbox"
Is this not possible or do I have to use another method?

If you have an Office365 subscription you could use the Office 365 api via their graph api endpoint
Since this is basically a REST endpoint you can use the Invoke-Webrequest or Invoke-RestMethod cmdlets.
Or more specificaly the Outlook api.
Both give you json back with you messages content like subject, to, from, and whatever.

Related

Does Connect-MsolService -Credential no longer work?

I'm attempting to create the ability to connect to my o365 instance through the use of connect-msolservice. Im running into the issue that no matter what I try Im unable to use -Credential to feed it a username and password and avoid a prompt. This is a massive issue as there is literally no point in attempting to automate a process unless it automates the connection too.
Currently what I'm trying to use:
$AdminName = "admin email account"
$Pass = Get-Content "C:\test.txt" | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
Import-Module MSOnline
Connect-MsolService -Credential $cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Everywhere I check online tells me that this should be possible and for the life of me I cant find anything obvious that I'm doing incorrectly, albeit, all the posts and info I do find are about a year old. This leads me to the question, is this simply no longer supported? If not and hopefully this is the case, what am I doing incorrectly?

How do we fetch shared mailbox list in powershell using service account credentials

Following is the script I used to fetch the shared mailbox list by getting adminId and password. But I want to fetch the details using the service account, can anyone help me out
$Credentials = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited | ft Name,WindowsEmailAddress

Error with add user in a group using Add-MsolGroupMember

I am trying to add user in a group but I am getting error:
Add-MsolGroupMember -GroupObjectId $group1.ObjectId -GroupMemberType "User" -GroupMemberObjectId 077cf65b-4b9f-44e4-9f34-6c96a063a0df
As bunzab said, we should use Add-DistributionGroupMember to add member.
First, we should connect to exchange online powershell:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
After that, we can use this powershell to add member, like this:
More information about connect to exchange online powershell, please refer to this link.

Remove-MsolGroupMember use exchange online to perform this operation

I get the error:
Remove-MsoLGroupMember : You cannot update mail-enabled groups using
this cmdlet. Use Exchange Online to perform this operation
But i have connected to a remote session and imported the exchange online module, why does it still throw error?
$credential = Get-Credential
$lyncSession = New-CsOnlineSession -Credential $credential
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Get-PSSession
Import-PSSession $exchangeSession
Remove-MsoLGroupMember -GroupObjectId $Group_GUID -GroupMemberType User -GroupmemberObjectId $GroupMembers.ObjectId
I was following this link to setup my remote connection to exchange, but i must be doing something wrong. https://technet.microsoft.com/en-us/library/dn362787(v=ocs.15).aspx
Use Remove-DistributionGroupMember instead if you are working with mail enabled security group as Remove-MsoLGroupMember works with regular security groups.
check this: https://technet.microsoft.com/en-us/library/aa998016(v=exchg.160).aspx

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)