# import MSOline Modules
import-module MSOnline
$cred=Get-Credential
# Connect to EXO
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
# Request Start date and check its validity
do
{
$Start= read-host "Please enter START date (ie: '06/30/2015'; date alone set time to 00:00):"
$Startchk = $Start -as [datetime]
if (!$startchk) { "Not A valid date and time"}
} while ($startchk -isnot [datetime])
# Request end date and check its validity
do
{
$End= read-host "Please enter END date (ie: '06/30/2015'; date alone set time to 00:00):"
$Endchk = $End -as [datetime]
if (!$endchk) { "Not A valid date and time"}
} while ($endchk -isnot [datetime])
#Format data from EXO
$Projection = `
@{Name='USER NAME';Expression={$_.UPN}},`
@{Name='OS';Expression={$_.Name}},`
@{Name='VERSION';Expression={$_.Version}},`
@{Name='DAYS USED';Expression={$_.Count}},`
@{Name='LAST ACCESSED DATE';Expression={$_.LastAccessTime}}
#Get Desktop folder for current user and create new folder
$MYLOGS=Read-Host "Please enter foldername (will pe put on your desktop) where logs will be saved"
$DOCDIR = [Environment]::GetFolderPath("Desktop")
$TARGETDIR = $DOCDIR+"\"+$MYLOGS
if(!(Test-Path -Path $TARGETDIR )){
New-Item -ItemType directory -Path $TARGETDIR
}
#Define results file
$outputfile=$TARGETDIR+"\O365ClientOSDetail.csv"
#Write output to file
Get-O365ClientOSDetailReport -StartDate $Start -EndDate $End `
-ResultSize Unlimited | `
Select $Projection | `
Export-Csv $OutputFile -NoTypeInformation
write-host -ForegroundColor Green "Logs have been saved to: " $outputfile
#Disconnect from EXO
Remove-PSSession $Session