Intune-Scripts icon indicating copy to clipboard operation
Intune-Scripts copied to clipboard

Suggestion: Optimize script

Open nicoride93 opened this issue 2 years ago • 0 comments

Hi! The script to collect the inventory is great!.

One suggestion, that I applied and is was much faster, specially if the device list is to big.

Instead of sending one app to log analytics at a time, send the collection of apps of one device. This is the code modification that I did

$totalDevices | ForEach-Object { $deviceHostname = $.deviceName $deviceManufacturer = $.Manufacturer $deviceModel = $.Model $deviceOs = $.OperatingSystem $deviceOsVersion = $.OSVersion $UserUpn = $.EmailAddress $device = Get-MgDeviceManagementManagedDevice -Expand detectedApps -ManagedDeviceId $.id $body = @() $device.detectedApps | ForEach-Object { $properties = [Ordered] @{ "UserUPN" = $UserUpn "Hostname" = $deviceHostname "Manufacturer" = $deviceManufacturer "Model" = $deviceModel "OperatingSystem" = $deviceOs "OSVersion" = $deviceOsVersion "AppName" = $.DisplayName "AppVersion" = $_.Version } $body += $properties } $sdeviceAppInventory = $body | ConvertTo-Json $params = @{ f_customerId = $customerId f_sharedKey = $sharedKey f_body = ([System.Text.Encoding]::UTF8.GetBytes($sdeviceAppInventory)) f_logType = $logType } $logResponse = Post-LogAnalyticsData @params "Working on device {0} - {1}/{2}....Apps:{3}....Response {4}" -f $deviceHostname, $counter, $totalDevices.count, $device.detectedApps.count, $logResponse $counter++ }

That will send per device one call to log analytics instead of 1 call per app. is much faster.

Thanks!

nicoride93 avatar Apr 11 '23 15:04 nicoride93