-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathInvoke-IntuneWin32AppRedeploy.ps1
288 lines (231 loc) · 12 KB
/
Invoke-IntuneWin32AppRedeploy.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
function Invoke-IntuneWin32AppRedeploy {
<#
.SYNOPSIS
Function for forcing redeploy of selected Win32App deployed from Intune.
.DESCRIPTION
Function for forcing redeploy of selected Win32App deployed from Intune.
OutGridView is used to output found Apps.
Redeploy means that corresponding registry keys will be deleted from registry and service IntuneManagementExtension will be restarted.
.PARAMETER computerName
Name of remote computer where you want to force the redeploy.
.PARAMETER getDataFromIntune
Switch for getting Apps and User names from Intune, so locally used IDs can be translated to them.
.PARAMETER credential
Credential object used for Intune authentication.
.PARAMETER tenantId
Azure Tenant ID for Intune App authentication.
.PARAMETER excludeSystemApp
Switch for excluding Apps targeted to SYSTEM.
.EXAMPLE
Invoke-IntuneWin32AppRedeploy
Get and show Win32App(s) deployed from Intune to this computer. Selected ones will be then redeployed.
.EXAMPLE
Invoke-IntuneWin32AppRedeploy -computerName PC-01 -getDataFromIntune credential $creds
Get and show Win32App(s) deployed from Intune to computer PC-01. IDs of apps and targeted users will be translated to corresponding names. Selected ones will be then redeployed.
.EXAMPLE
Invoke-IntuneWin32AppRedeploy -computerName PC-01 -getDataFromIntune credential $creds -tenantId 123456789
Get and show Win32App(s) deployed from Intune to computer PC-01. App authentication will be used instead of user auth.
IDs of apps and targeted users will be translated to corresponding names. Selected ones will be then redeployed.
.NOTES
Author: @AndrewZtrhgf
#>
[CmdletBinding()]
param (
[string] $computerName,
[switch] $getDataFromIntune,
[System.Management.Automation.PSCredential] $credential,
[string] $tenantId,
[switch] $excludeSystemApp
)
#region helper function
function _getTargetName {
param ([string] $id)
Write-Verbose "Translating $id"
if (!$id) {
Write-Verbose "id was null"
return
} elseif ($id -eq 'device') {
# xml nodes contains 'device' instead of 'Device'
return 'Device'
}
$errPref = $ErrorActionPreference
$ErrorActionPreference = "Stop"
try {
if ($id -eq '00000000-0000-0000-0000-000000000000' -or $id -eq 'S-0-0-00-0000000000-0000000000-000000000-000') {
return 'Device'
} elseif ($id -match "^S-1-5-21") {
# it is local account
return ((New-Object System.Security.Principal.SecurityIdentifier($id)).Translate([System.Security.Principal.NTAccount])).Value
} else {
# it is AzureAD account
if ($getDataFromIntune) {
return ($intuneUser | ? id -EQ $id).userPrincipalName
} else {
# unable to translate ID to name because there is no connection to the Intune Graph API
return $id
}
}
} catch {
Write-Warning "Unable to translate $id to account name ($_)"
$ErrorActionPreference = $errPref
return $id
}
}
function _getIntuneApp {
param ([string] $appID)
$intuneApp | ? id -EQ $appID
}
# create helper functions text definition for usage in remote sessions
if ($computerName) {
$allFunctionDefs = "function _getTargetName { ${function:_getTargetName} }; function _getIntuneApp { ${function:_getIntuneApp} }"
}
#endregion helper function
#region prepare
if ($getDataFromIntune) {
if (!(Get-Module 'Microsoft.Graph.Intune') -and !(Get-Module 'Microsoft.Graph.Intune' -ListAvailable)) {
throw "Module 'Microsoft.Graph.Intune' is required. To install it call: Install-Module 'Microsoft.Graph.Intune' -Scope CurrentUser"
}
if ($tenantId) {
# app logon
if (!$credential) {
$credential = Get-Credential -Message "Enter AppID and AppSecret for connecting to Intune tenant" -ErrorAction Stop
}
Update-MSGraphEnvironment -AppId $credential.UserName -Quiet
Update-MSGraphEnvironment -AuthUrl "https://login.windows.net/$tenantId" -Quiet
$null = Connect-MSGraph -ClientSecret $credential.GetNetworkCredential().Password -ErrorAction Stop
} else {
# user logon
if ($credential) {
$null = Connect-MSGraph -Credential $credential -ErrorAction Stop
# $header = New-GraphAPIAuthHeader -credential $credential -ErrorAction Stop
} else {
$null = Connect-MSGraph -ErrorAction Stop
# $header = New-GraphAPIAuthHeader -ErrorAction Stop
}
}
Write-Verbose "Getting Intune data"
# filtering by ID is as slow as getting all data
# Invoke-MSGraphRequest -Url 'https://graph.microsoft.com/beta/deviceAppManagement/mobileApps?$filter=(id%20eq%20%2756695a77-925a-4df0-be79-24ed039afa86%27)'
$intuneApp = Invoke-MSGraphRequest -Url "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps?select=id,displayname" | Get-MSGraphAllPages
$intuneUser = Invoke-MSGraphRequest -Url 'https://graph.microsoft.com/beta/users?select=id,userPrincipalName' | Get-MSGraphAllPages
}
if ($computerName) {
$session = New-PSSession -ComputerName $computerName -ErrorAction Stop
} else {
if (! ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
throw "Run as administrator"
}
}
#endregion prepare
#region get data
$scriptBlock = {
param($verbosePref, $excludeSystemApp, $getDataFromIntune, $intuneApp, $intuneUser, $allFunctionDefs)
# inherit verbose settings from host session
$VerbosePreference = $verbosePref
# recreate functions from their text definitions
. ([ScriptBlock]::Create($allFunctionDefs))
foreach ($app in (Get-ChildItem "HKLM:\SOFTWARE\Microsoft\IntuneManagementExtension\Win32Apps" -ErrorAction SilentlyContinue)) {
$userAzureObjectID = Split-Path $app.Name -Leaf
if ($excludeSystemApp -and $userAzureObjectID -eq "00000000-0000-0000-0000-000000000000") {
Write-Verbose "Skipping system deployments"
continue
}
$userWin32AppRoot = $app.PSPath
$win32AppIDList = Get-ChildItem $userWin32AppRoot | select -ExpandProperty PSChildName | % { $_ -replace "_\d+$" } | select -Unique
$win32AppIDList | % {
$win32AppID = $_
Write-Verbose "Processing App ID $win32AppID"
$newestWin32AppRecord = Get-ChildItem $userWin32AppRoot | ? PSChildName -Match ([regex]::escape($win32AppID)) | Sort-Object -Descending -Property PSChildName | select -First 1
$lastUpdatedTimeUtc = Get-ItemPropertyValue $newestWin32AppRecord.PSPath -Name LastUpdatedTimeUtc
try {
$complianceStateMessage = Get-ItemPropertyValue "$($newestWin32AppRecord.PSPath)\ComplianceStateMessage" -Name ComplianceStateMessage -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
} catch {
Write-Verbose "`tUnable to get Compliance State Message data"
}
try {
$enforcementStateMessage = Get-ItemPropertyValue "$($newestWin32AppRecord.PSPath)\EnforcementStateMessage" -Name EnforcementStateMessage -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
} catch {
Write-Verbose "`tUnable to get Enforcement State Message data"
}
$lastError = $complianceStateMessage.ErrorCode
if (!$lastError) { $lastError = 0 } # because of HTML conditional formatting ($null means that cell will have red background)
if ($getDataFromIntune) {
$property = [ordered]@{
"Scope" = _getTargetName $userAzureObjectID
"DisplayName" = (_getIntuneApp $win32AppID).DisplayName
"Id" = $win32AppID
"LastUpdatedTimeUtc" = $lastUpdatedTimeUtc
# "Status" = $complianceStateMessage.ComplianceState
"ProductVersion" = $complianceStateMessage.ProductVersion
"LastError" = $lastError
"ScopeId" = $userAzureObjectID
}
} else {
# no 'DisplayName' property
$property = [ordered]@{
"ScopeId" = _getTargetName $userAzureObjectID
"Id" = $win32AppID
"LastUpdatedTimeUtc" = $lastUpdatedTimeUtc
# "Status" = $complianceStateMessage.ComplianceState
"ProductVersion" = $complianceStateMessage.ProductVersion
"LastError" = $lastError
}
}
New-Object -TypeName PSObject -Property $property
}
}
}
$param = @{
scriptBlock = $scriptBlock
argumentList = ($VerbosePreference, $excludeSystemApp, $getDataFromIntune, $intuneApp, $intuneUser, $allFunctionDefs)
}
if ($computerName) {
$param.session = $session
}
$win32App = Invoke-Command @param | select -Property * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName
#endregion get data
#region let user redeploy chosen app
if ($win32App) {
$hasDisplayNameProp = $win32App | Get-Member -Name DisplayName
$appToRedeploy = $win32App | ? { if ($hasDisplayNameProp) { if ($_.DisplayName) { $true } } else { $true } } | Out-GridView -PassThru -Title "Pick app(s) for redeploy"
if ($appToRedeploy) {
$scriptBlock = {
param ($verbosePref, $appToRedeploy)
# inherit verbose settings from host session
$VerbosePreference = $verbosePref
$win32AppKeys = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\IntuneManagementExtension\Win32Apps" -Recurse -Depth 2 | select PSChildName, PSPath, PSParentPath
$appToRedeploy | % {
$appId = $_.id
$scopeId = $_.scopeId
if ($scopeId -eq 'device') { $scopeId = "00000000-0000-0000-0000-000000000000" }
Write-Warning "Preparing redeploy for app $appId (scope $scopeId)"
$win32AppKeyToDelete = $win32AppKeys | ? { $_.PSChildName -Match "^$appId`_\d+" -and $_.PSParentPath -Match "\\$scopeId$" }
if ($win32AppKeyToDelete) {
$win32AppKeyToDelete | % {
Write-Verbose "Deleting $($_.PSPath)"
Remove-Item $_.PSPath -Force -Recurse
}
} else {
throw "BUG??? App $appId with scope $scopeId wasn't found in the registry"
}
}
Write-Warning "Invoking redeploy (by restarting service IntuneManagementExtension). Redeploy can take several minutes!"
Restart-Service IntuneManagementExtension -Force
}
$param = @{
scriptBlock = $scriptBlock
argumentList = ($VerbosePreference, $appToRedeploy)
}
if ($computerName) {
$param.session = $session
}
Invoke-Command @param
}
} else {
Write-Warning "No deployed Win32App detected"
}
#endregion let user redeploy chosen app
if ($computerName) {
Remove-PSSession $session
}
}