Office 365 powershell

Hi,

I am looking to export a list of enabled users in Office365 to csv who haven't sent an email in over 90 days, I started working on a script but keep getting errors, any suggestion?

$UserStats = Get-mailbox -resultsize unlimited | Get-MailboxStatistics | where {$_.LastLogonTime -lt ((get-date).AddDays(-90))} | Get-MsolUser -EnabledFilter EnabledOnly | 
Select userprincipalname,Licenses,lastlogontime > c:\ps\export.txt

Open in new window

Ben SmithAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dope ADMSr. Systems EngineerCommented:
Correction: Please disregard the above, reviewing the select part....will update you shortly
0
Dope ADMSr. Systems EngineerCommented:
Try this and let me know if it works:

$CollectUsers = Get-mailbox -resultsize unlimited -RecipientTypeDetails UserMailbox,SharedMailbox |Get-MailboxStatistics |where {$_.LastLogonTime -lt ((get-date).AddDays(-90))} 
$UserStats = @()

foreach($user in $CollectUsers){
$obj = "" | select userprincipalname,Licenses,LastLogonTime
$x = Get-MsolUser -UserPrincipalName (Get-Mailbox $user.MailboxGuid.Guid).UserPrincipalName | ? {$_.isLicensed -and !($_.BlockCredential)}
$obj.LastLogonTime = $user.LastLogonTime
$obj.userprincipalname = $x.userprincipalname
$obj.Licenses = $x.Licenses
$UserStats += $obj
}
$UserStats | Out-File c:\ps\export.txt

Open in new window

0
Jose Gabriel Ortega CastroCEOCommented:
You are piping 2 differents Cmdlets from 2 different "sources".

O365 contains the "MsolService" conenction that is what you see in the portal of o365 administration.
But the Get-mailbox is only available in the "Exchange Online" Environment.

So that way will never work bud.
How should you go then?
1st Solve the Exchange Online to find out those who haven't logged in into the last 90 days,
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$Inactive90=Get-mailbox -resultsize unlimited |select * | Get-MailboxStatistics | where {$_.LastLogonTime -lt ((get-date).AddDays(-90))} | select DisplayName

Open in new window


This way on the var "Inactive90"  (that only brings DisplayName as an important field.


The end result will be like:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

$Result=@()
$Inactive90= Get-Mailbox -resultsize unlimited | Get-MailboxStatistics  | where {$_.LastLogonTime -lt ((get-date).AddDays(-90))} 
Connect-MsolService -credential $UserCredential 
foreach($user in $Inactive90){
	$search=Get-MsolUser -EnabledFilter EnabledOnly | where{ $_.DisplayName -eq $user.DisplayName} | select userprincipalname,Licenses,lastlogontime
	if(!($search)){
		$Result+=$search
	}
}

$Result| ConvertTo-Csv -NoTypeInformation file.csv

Open in new window

0
Webinar: Cyber Crime Becomes Big Business

The rising threat of malware-as-a-service is not one to be overlooked. Malware-as-a-service is growing and easily purchased from a full-service cyber-criminal store in a “Virus Depot” fashion. Join us in our upcoming webinar as we discuss how to best defend against these attacks!

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
{ $_.LastLogonTime -lt (get-date).AddDays(-90) } isn't the same as "haven't sent an email in over 90 days". The user might have read emails, and therefor needed to log in.
1
Vasil Michev (MVP)Commented:
You know there's a built-in report for that, right? O365 Admin portal -> Reports -> Usage -> Email activity or directly via https://portal.office.com/adminportal/home#/reportsUsage/EmailActivity
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PberSolutions ArchitectCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Vasil Michev (MVP) (https:#a42429608)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.