Forum Discussion
RichDadufalza
Oct 27, 2021Copper Contributor
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
- AndySvintsSteel 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:
- Identity
- DeviceType
- DeviceOS
- LastSuccessSync
- LastSyncAttemptTime
- LastPolicyUpdateTime
- LastPingHeartbeat
- ClientType
and not:
- Name
- UPN
- FriendlyName
- DisplayName
- ClientVersion
- DeviceId
- DeviceMobileOperator
- DeviceModel
- DeviceTelephoneNumber
- DeviceType
- FirstSyncTime
- 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