IntuneDeviceApplicationFind.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<#PSScriptInfo
 
.VERSION 1.2
 
.GUID fd6ec058-2c2b-4ca0-a03c-4d4984e01234
 
.AUTHOR Vikas Sukhija
 
.COMPANYNAME TechWizard
 
.COPYRIGHT Vikas Sukhija
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI http://techwizard.cloud/2020/08/17/intune-check-particular-app-installation-on-devices
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
http://techwizard.cloud/2020/08/17/intune-check-particular-app-installation-on-devices
 
.PRIVATEDATA
Update: 8/27/2020 fixe dteh bug of folder creation
Update: 10/18/2021 Get-IntuneManagedDevice | Get-MSGraphAllPages stopped working so change to pagination
update: 12/2/2021 changed to filter as MS has made a change in API
#>


<#
 
.DESCRIPTION
 Find and report particular application installed on devices
 
#>
 
param (
  [Parameter(Mandatory = $true)]
  [string]$Application
  )

function New-FolderCreation
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory = $true)]
    [string]$foldername
  )
    

  $logpath  = (Get-Location).path + "\" + "$foldername" 
  $testlogpath = Test-Path -Path $logpath
  if($testlogpath -eq $false)
  {
    #Start-ProgressBar -Title "Creating $foldername folder" -Timer 10
    $null = New-Item -Path (Get-Location).path -Name $foldername -Type directory
  }
}####new folder creation

function Write-Log
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory = $true,ParameterSetName = 'Create')]
    [array]$Name,
    [Parameter(Mandatory = $true,ParameterSetName = 'Create')]
    [string]$Ext,
    [Parameter(Mandatory = $true,ParameterSetName = 'Create')]
    [string]$folder,
    
    [Parameter(ParameterSetName = 'Create',Position = 0)][switch]$Create,
    
    [Parameter(Mandatory = $true,ParameterSetName = 'Message')]
    [String]$message,
    [Parameter(Mandatory = $true,ParameterSetName = 'Message')]
    [String]$path,
    [Parameter(Mandatory = $false,ParameterSetName = 'Message')]
    [ValidateSet('Information','Warning','Error')]
    [string]$Severity = 'Information',
    
    [Parameter(ParameterSetName = 'Message',Position = 0)][Switch]$MSG
  )
  switch ($PsCmdlet.ParameterSetName) {
    "Create"
    {
      $log = @()
      $date1 = Get-Date -Format d
      $date1 = $date1.ToString().Replace("/", "-")
      $time = Get-Date -Format t
    
      $time = $time.ToString().Replace(":", "-")
      $time = $time.ToString().Replace(" ", "")
      New-FolderCreation -foldername $folder
      foreach ($n in $Name)
      {$log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext"}
      return $log
    }
    "Message"
    {
      $date = Get-Date
      $concatmessage = "|$date" + "| |" + $message +"| |" + "$Severity|"
      switch($Severity){
        "Information"{Write-Host -Object $concatmessage -ForegroundColor Green}
        "Warning"{Write-Host -Object $concatmessage -ForegroundColor Yellow}
        "Error"{Write-Host -Object $concatmessage -ForegroundColor Red}
      }
      
      Add-Content -Path $path -Value $concatmessage
    }
  }
} #Function Write-Log

####################Load variables and log####################
$log = Write-Log -Name "IntuneDeviceApplication-Log" -folder "logs" -Ext "log"
$Report = Write-Log -Name "IntuneDeviceApplication-Report" -folder "Report" -Ext "csv"
$collection = @()
$graphApiVersion = "beta"
################connect to modules###################
Write-Log -Message "Start............Script" -path $log
try
{
  Connect-MSGraph
  Write-Log -Message "Intune Module Loaded" -path $log
}
catch
{
  $exception = $_.Exception
  Write-Log -Message "Error loading AD/Intune Module Loaded" -path $log -Severity Error 
  Write-Log -Message $exception -path $log -Severity error
  Exit
}

##########process devicess with compliant state as unknown####
try
{
  Write-Log -Message "Start fetching all devices in the enviornment, it could take a while" -path $log 
  $getalldevices = Get-IntuneManagedDevice -Filter "managementagent eq 'mdm'" | Get-MSGraphAllPages 
  Write-Log -Message "Count of devices $($getalldevices.count)" -path $log 
}
catch
{
  $exception = $_.Exception
  Write-Log -Message "Error occured fetching unkown devices" -path $log -Severity Error 
  Write-Log -Message $exception -path $log -Severity error
  Exit
}
###########Remove these devices from Intune#################
$count=0
$getalldevices |
ForEach-Object{
  $mcoll = "" |
  Select-Object id, deviceName, AppName, Appversion, Appid, SizeinByte, enrolledDateTime, lastSyncDateTime, emailAddress, serialNumber, complianceState
  $mcoll.id = $_.id
  $deviceid = $_.id
  $Resource = "deviceManagement/managedDevices" + "/" + "$deviceid" + "?`$expand=detectedApps"
  $uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)"
  $app = Invoke-MSGraphRequest -HttpMethod GET -Url $uri
  $foundApp = $app.detectedApps |
  Where-Object{$_.displayname -like $Application}
  $mcoll.appname = $foundApp.displayname
  $mcoll.Appversion = $foundApp.Version
  $mcoll.SizeInByte = $foundApp.SizeInByte
  $mcoll.Appid = $foundApp.id
  $mcoll.deviceName = $_.deviceName
  $mcoll.enrolledDateTime = $_.enrolledDateTime
  $mcoll.lastSyncDateTime = $_.lastSyncDateTime
  $mcoll.emailAddress = $_.emailAddress
  $mcoll.serialNumber = $_.serialNumber
  $mcoll.complianceState = $_.complianceState
  $collection += $mcoll
  $count=$count+1
  if($_.deviceName){
    Write-Progress -Activity "Finding $Application" -status "$($_.deviceName)" -percentComplete ($count/$getalldevices.count*100)
  }else{
   Write-Progress -Activity "Finding $Application" -status "Device Not Found" -percentComplete ($count/$getalldevices.count*100)
  }
}
$collection | where{$_.appname -like $Application} | Export-Csv $Report -NoTypeInformation
 
Write-Log -Message "Script Finished" -path $log
################################################################################