I have this code the works fine but because it takes so long to query it, I made some modifications.
This is the working code:
$Devices = Get-MsolDevice -All -ReturnRegisteredOwners -IncludeSystemManagedDevices
$DeviceInfo = @()
foreach ($Device in $Devices) {
$DeviceInfo += [PSCustomObject]@{
"DisplayName" = $Device.DisplayName
"DeviceTrustType" = $Device.DeviceTrustType
"DeviceTrustLevel" = $Device.DeviceTrustLevel
"DeviceOS" = $Device.DeviceOsType
"DeviceVersion" = $Device.DeviceOsVersion
"RegisteredOwner" = $($Device.RegisteredOwners)
"LastLogon" = $Device.ApproximateLastLogonTimestamp
"LastDirSync" = $Device.LastDirSyncTime
"DeviceID" = $Device.DeviceId
"ObjectID" = $Device.ObjectId
}
}
This is the modified code, in which I have another csv $allobjects that contains objectID of Devices in Windows. By doing this, I have cut the amount of devices I need to query into half If I use the code above.
$a = @()
ForEach ($line in $allobjects)
{
$device = Get-MsolDevice -ObjectID $line.ObjectId | ? {$_.deviceos -eq "Windows"} | Select DisplayName,DeviceTrustType,ApproximateLastLogonTimestamp,RegisteredOwners
$a += New-Object psobject -property @{
"DisplayName" = $device.displayname
"DeviceTrustType" = $device.devicetrusttype
"ApproximateLastLogon" = $device.ApproximateLastLogonTimestamp
"RegisteredOwner" = $($Device.RegisteredOwners)}
}
$a | export-csv "C:\temp\alldevices.csv" -noTypeInformation
However, I can't seem to get the CSV right.