Forum Discussion

RichDadufalza's avatar
RichDadufalza
Copper Contributor
Oct 27, 2021

How to configure output to .csv?

Hello all,

I'm new to using Powershell. I found this script online to help display who has a mobile device connected to O365. The script runs fine in PS, but I would like to output the results to .csv and I am not sure how to do that. I tried editing another script I found online, but no success. Any help is greatly appreciated. Here is what I am currently using.

 

$credentials = Get-Credential -Credential blank@blank.onmicrosoft.com
Write-Output "Getting the Exchange Online cmdlets"

$session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-ConfigurationName Microsoft.Exchange -Credential $credentials `
-Authentication Basic -AllowRedirection
Import-PSSession $session

$csv = "C:\temp\MobileDevicesStatistics.csv"
$results = @()
$mailboxUsers = get-mailbox -resultsize unlimited
$mobileDevice = @()

foreach($user in $mailboxUsers)
{
$UPN = $user.UserPrincipalName
$displayName = $user.DisplayName

$mobileDeviceList = Get-MobileDevice -Mailbox $UPN

foreach ($Device in $MobileDeviceList) {
$Stats = Get-MobileDeviceStatistics -Identity $Device.Guid.toString()
[PSCustomObject]@{
Identity = $Device.Identity -replace "\\.+"
DeviceType = $Device.DeviceType
DeviceOS = $Device.DeviceOS
LastSuccessSync = $Stats.LastSuccessSync
LastSyncAttemptTime = $Stats.LastSyncAttemptTime
LastPolicyUpdateTime = $Stats.LastPolicyUpdateTime
LastPingHeartbeat = $Stats.LastPingHeartbeat
ClientType = $Stats.ClientType
}
$results += New-Object psobject -Property $properties
}
}

$results | Select-Object Name,UPN,FriendlyName,DisplayName,ClientType,ClientVersion,DeviceId,DeviceMobileOperator,DeviceModel,DeviceOS,DeviceTelephoneNumber,DeviceType,FirstSyncTime, LastSuccessSync, LastSyncAttemptTime, LastPolicyUpdateTime, LastPingHeartbeat, UserDisplayName | Export-Csv -notypeinformation -Path $csv

Remove-PSSession $session

  • AndySvints's avatar
    AndySvints
    Steel Contributor

    Hello RichDadufalza,

    You are experiencing problems with your CSV output because your $results collection does not contain all of the properties that you selected.

    It contains only:

    1. Identity
    2. DeviceType
    3. DeviceOS
    4. LastSuccessSync
    5. LastSyncAttemptTime
    6. LastPolicyUpdateTime
    7. LastPingHeartbeat
    8. ClientType

    and not:

    1. Name
    2. UPN
    3. FriendlyName
    4. DisplayName
    5. ClientVersion
    6. DeviceId
    7. DeviceMobileOperator
    8. DeviceModel
    9. DeviceTelephoneNumber
    10. DeviceType
    11. FirstSyncTime
    12. UserDisplayName

    Your resulting object is missing 12 properties.

    Quick adjustment of your script that will give you some output:

    $credentials = Get-Credential -Credential blank@blank.onmicrosoft.com
    Write-Output "Getting the Exchange Online cmdlets"
    
    $session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
    -ConfigurationName Microsoft.Exchange -Credential $credentials `
    -Authentication Basic -AllowRedirection
    Import-PSSession $session
    
    $csv = "C:\temp\MobileDevicesStatistics.csv"
    $results = @()
    $mailboxUsers = get-mailbox -resultsize unlimited
    
    
    foreach($user in $mailboxUsers)
    {
        $UPN = $user.UserPrincipalName
        $displayName = $user.DisplayName
    
        $mobileDeviceList = Get-MobileDevice -Mailbox $UPN
    
        foreach ($Device in $MobileDeviceList) {
            $Stats = Get-MobileDeviceStatistics -Identity $Device.Guid.toString()
                
            $properties=[ordered]@{
                Name=$displayName
                UPN=$UPN
                Identity = $Device.Identity -replace "\\.+"
                DeviceType = $Device.DeviceType
                DeviceOS = $Device.DeviceOS
                LastSuccessSync = $Stats.LastSuccessSync
                LastSyncAttemptTime = $Stats.LastSyncAttemptTime
                LastPolicyUpdateTime = $Stats.LastPolicyUpdateTime
                LastPingHeartbeat = $Stats.LastPingHeartbeat
                ClientType = $Stats.ClientType
            }
            $results += New-Object psobject -Property $properties
    
        }
    }
    
    $results | Export-Csv -notypeinformation -Path $csv
    
    Remove-PSSession $session

    Hope that helps

     

     

     

Resources