Get-PsIntuneInstalledApps.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function Get-psIntuneInstalledApps {
    <#
    .SYNOPSIS
        Returns App inventory data from Intune Device data set
 
    .DESCRIPTION
        Returns App inventory data from Intune Device data set
 
    .PARAMETER DataSet
        Data returned from Get-psIntuneDevice
 
    .EXAMPLE
        $devices = Get-psIntuneDevice -UserName "john.doe@contoso.com"
        $applist = Get-psIntuneInstalledApps -DataSet $devices
 
    .NOTES
        NAME: Get-psIntuneInstalledApps
 
    .LINK
        https://github.com/Skatterbrainz/psintune/blob/master/docs/Get-psIntuneInstalledApps.md
 
    #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory)][ValidateNotNull()] $DataSet,
        [parameter()][boolean] $GroupByName = $False
    )
    $badnames = ('. .','. . .','..','...')
    Write-Verbose "reading $($DataSet.Count) objects"
    [int]$appcount = 0
    $result = $DataSet | Foreach-Object {
        [string]$devicename = $_.DeviceName
        $apps = $_.Apps 
        if ($null -ne $Apps) {
            foreach ($app in $apps) {
                $displayName = $($app.displayName).ToString().Trim()
                if (![string]::IsNullOrEmpty($displayName)) {
                    if ($displayName -notin $badnames) {
                        if ($($app.Id).Length -gt 36) {
                            [string]$ptype = 'WindowsStore'
                        }
                        elseif ($($app.Id).Length -eq 36) {
                            [string]$ptype = 'Win32'
                        }
                        else {
                            [string]$ptype = 'Other'
                        }
                        [pscustomobject]@{
                            ProductName    = $displayName
                            ProductVersion = $($app.version).ToString().Trim()
                            ProductCode    = $app.Id
                            ProductType    = $ptype
                            DeviceName     = $devicename
                        }
                    }
                }
            }
            $appcount++
        }
        else {
            Write-Verbose "$devicename - has no apps"
        }
    }
    if ($appcount -eq 0) {
        Write-Warning "DataSet objects have no applications linked. Use [-Detail Full] option with Get-psIntuneDevice"
    }
    if ($GroupByName -eq $True) {
        $result | Group-Object -Property ProductName | Select-Object Count,Name | Sort-Object Name -Unique
    }
    else {
        $result
    }
}