Hello,
I have a script that I have created to pull a list of all the Mobile devices and their statistics from our Office 365 environment. When I execute this script manually, it completes without error and my report is generated. When I create a scheduled job to run the script, it hangs at connecting to the MSOLservice. I opened a ticket with O365 support and they reported that since it is a custom script they are unable to assist. Have any of you ran into this type of issue with O365? Are you able to run scheduled jobs to pull data on a regular basis?
Thanks in advance, here is the script.
<#
.Synopsis
Generates list of all MobileDeviceStats for devices in Office 365.
.DESCRIPTION
Generates list of all MobileDeviceStats for devices in Office 365.
.EXAMPLE
.\Get-MobileDeviceStats_v4.ps1
.NOTES
This script utilizes the ActiveDirectory PowerShell module, which must be available on the launching system.
This can be obtained from the Remote Server Administration Tools (RSAT) by Microsoft.
A RSAT for Windows 8 can be obtained here: http://www.microsoft.com/en-us/download/details.aspx?id=28972
#>
Import-Module MSOnlineExtended
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 1 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has started.'
#STORED CREDENTIAL CODE
$AdminName="admin@domain.onmicrosoft.com"
$CredsFile="C:\ps\MobileDeviceStats\$AdminName-PowershellCreds.txt"
$FileExists=Test-Path$CredsFile
if ($FileExists-eq$false) {
Write-Host'Credential file not found. Enter your password:'-ForegroundColor'Yellow'
Read-Host-AsSecureString | ConvertFrom-SecureString | Out-File$CredsFile
$password=Get-Content$CredsFile | ConvertTo-SecureString
$Cred=New-Object-TypeNameSystem.Management.Automation.PSCredential-Argumentlist$AdminName,$password}
else
{Write-Host'Using your stored credential file'-ForegroundColor'Green'
$password=Get-Content$CredsFile | ConvertTo-SecureString
$Cred=New-Object-TypeNameSystem.Management.Automation.PSCredential-ArgumentList$AdminName,$password}
#Create remote session for O365 and Azure AD
$Session=New-PSSession-ConfigurationNameMicrosoft.Exchange-ConnectionUri https://ps.outlook.com/powershell/-Credential$Cred-Authentication'Basic'-AllowRedirection
Import-PSSession$Session
Import-Module MSOnline
Connect-MsolService-Credential$Cred
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 2 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has initiated the remote session.'
#Set variables
$ExportedFilePath1="C:\ps\MobileDeviceStats\Reports\MobileDeviceStatistics_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$Script:ExportedFile=$ExportedFilePath1
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 3 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has successfully set the variables'
#Send email function
function sendmail
{
Send-MailMessage-To"user@domain.com" `
-From"PowerShell@domain.com" `
-Subject"Mobile Device Statistics" `
-Priority'Normal' `
-Attachments$Script:ExportedFile `
-Body"The attached CSV file contains the current list of Mobile Devices and their corresponding statistics.
Best Regards,
PowerShell
" `
-SmtpServersmtp.domain.com
}
#Send email update function
function sendupdate
{
Send-MailMessage-To"User2@domain.com" `
-From"PowerShell@domain.com" `
-Subject"Mobile Device Statistics" `
-Priority'Normal' `
-Body"The Get-MobileDeviceStats script has successfully completed and all remote sessions have been cleared.
Best Regards,
Your Friendly PowerShell Nerd
" `
-SmtpServersmtp.domain.com
}
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 4 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, is beginning the data gathering process.'
#
#Add sleep http://support2.microsoft.com/kb/2881759
#
$MB=Get-MobileDevice-Resultsize unlimited
$MB | foreach {Get-MobileDeviceStatistics$_.identity; Start-Sleep-Milliseconds 2000} | Select-Object FirstSyncTime,LastPolicyUpdateTime,LastSyncAttemptTime,LastSuccessSync,DeviceType,DeviceID,DeviceModel,DeviceFriendlyName,DeviceOS,DeviceOSLanguage,Identity,DeviceAccessState,NumberOfFoldersSynced |
Export-Csv-NoTypeInformation$Script:ExportedFile
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 5 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has completed the data gathering process.'
sendmail
Get-PSSession | Remove-PSSession
sendupdate
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 6 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has completed.'
.Synopsis
Generates list of all MobileDeviceStats for devices in Office 365.
.DESCRIPTION
Generates list of all MobileDeviceStats for devices in Office 365.
.EXAMPLE
.\Get-MobileDeviceStats_v4.ps1
.NOTES
This script utilizes the ActiveDirectory PowerShell module, which must be available on the launching system.
This can be obtained from the Remote Server Administration Tools (RSAT) by Microsoft.
A RSAT for Windows 8 can be obtained here: http://www.microsoft.com/en-us/download/details.aspx?id=28972
#>
Import-Module MSOnlineExtended
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 1 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has started.'
#STORED CREDENTIAL CODE
$AdminName="admin@domain.onmicrosoft.com"
$CredsFile="C:\ps\MobileDeviceStats\$AdminName-PowershellCreds.txt"
$FileExists=Test-Path$CredsFile
if ($FileExists-eq$false) {
Write-Host'Credential file not found. Enter your password:'-ForegroundColor'Yellow'
Read-Host-AsSecureString | ConvertFrom-SecureString | Out-File$CredsFile
$password=Get-Content$CredsFile | ConvertTo-SecureString
$Cred=New-Object-TypeNameSystem.Management.Automation.PSCredential-Argumentlist$AdminName,$password}
else
{Write-Host'Using your stored credential file'-ForegroundColor'Green'
$password=Get-Content$CredsFile | ConvertTo-SecureString
$Cred=New-Object-TypeNameSystem.Management.Automation.PSCredential-ArgumentList$AdminName,$password}
#Create remote session for O365 and Azure AD
$Session=New-PSSession-ConfigurationNameMicrosoft.Exchange-ConnectionUri https://ps.outlook.com/powershell/-Credential$Cred-Authentication'Basic'-AllowRedirection
Import-PSSession$Session
Import-Module MSOnline
Connect-MsolService-Credential$Cred
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 2 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has initiated the remote session.'
#Set variables
$ExportedFilePath1="C:\ps\MobileDeviceStats\Reports\MobileDeviceStatistics_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$Script:ExportedFile=$ExportedFilePath1
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 3 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has successfully set the variables'
#Send email function
function sendmail
{
Send-MailMessage-To"user@domain.com" `
-From"PowerShell@domain.com" `
-Subject"Mobile Device Statistics" `
-Priority'Normal' `
-Attachments$Script:ExportedFile `
-Body"The attached CSV file contains the current list of Mobile Devices and their corresponding statistics.
Best Regards,
PowerShell
" `
-SmtpServersmtp.domain.com
}
#Send email update function
function sendupdate
{
Send-MailMessage-To"User2@domain.com" `
-From"PowerShell@domain.com" `
-Subject"Mobile Device Statistics" `
-Priority'Normal' `
-Body"The Get-MobileDeviceStats script has successfully completed and all remote sessions have been cleared.
Best Regards,
Your Friendly PowerShell Nerd
" `
-SmtpServersmtp.domain.com
}
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 4 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, is beginning the data gathering process.'
#
#Add sleep http://support2.microsoft.com/kb/2881759
#
$MB=Get-MobileDevice-Resultsize unlimited
$MB | foreach {Get-MobileDeviceStatistics$_.identity; Start-Sleep-Milliseconds 2000} | Select-Object FirstSyncTime,LastPolicyUpdateTime,LastSyncAttemptTime,LastSuccessSync,DeviceType,DeviceID,DeviceModel,DeviceFriendlyName,DeviceOS,DeviceOSLanguage,Identity,DeviceAccessState,NumberOfFoldersSynced |
Export-Csv-NoTypeInformation$Script:ExportedFile
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 5 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has completed the data gathering process.'
sendmail
Get-PSSession | Remove-PSSession
sendupdate
Write-EventLog-LogName Application -Source'MobileDeviceStats Script'-EntryType'Information'-EventId 6 -Message'The PowerShell script, Get-MobileDeviceStats.PS1, has completed.'