-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathTest-IntuneWin32AppAssignment.ps1
More file actions
98 lines (87 loc) · 4.28 KB
/
Copy pathTest-IntuneWin32AppAssignment.ps1
File metadata and controls
98 lines (87 loc) · 4.28 KB
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
function Test-IntuneWin32AppAssignment {
<#
.SYNOPSIS
Test the presence of an existing assignment type for a Win32 app.
.DESCRIPTION
Test the presence of an existing assignment type for a Win32 app.
.PARAMETER ID
Specify the ID of the Win32 app.
.PARAMETER Target
Specify the target type of the assignment, AllDevices, AllUsers or Group.
.NOTES
Author: Nickolaj Andersen
Contact: @NickolajA
Created: 2020-09-21
Updated: 2020-09-21
Version history:
1.0.0 - (2020-09-21) Function created
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[parameter(Mandatory = $true, HelpMessage = "Specify the ID of the Win32 app.")]
[ValidateNotNullOrEmpty()]
[string]$ID,
[parameter(Mandatory = $false, HelpMessage = "Specify the DataType in this assignment")]
[ValidateNotNullOrEmpty()]
[string]$DataType,
[parameter(Mandatory = $false, HelpMessage = "Specify the target type of the assignment, AllDevices, AllUsers or Group.")]
[ValidateNotNullOrEmpty()]
[ValidateSet("AllDevices", "AllUsers", "Group")]
[string]$Target
)
Process {
# Handle initial value for duplicate assignment
$DuplicateAssignmentDetected = $false
try {
Write-Verbose -Message "Retrieving any existing Win32 app assignments to validate existing assignments for duplicate resources"
$Win32AppAssignments = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps/$($ID)/assignments" -Method "GET" -ErrorAction Stop
$Win32AppAssignmentsCount = ($Win32AppAssignments.value | Measure-Object).Count
if ($Win32AppAssignmentsCount -ge 1) {
Write-Verbose -Message "Detected count of '$($Win32AppAssignmentsCount)' existing assignments, processing each item for validation"
# Define target types for AllDevices and AllUsers
switch ($Target) {
"AllDevices" {
$TargetType = "allDevicesAssignmentTarget"
}
"AllUsers" {
$TargetType = "allLicensedUsersAssignmentTarget"
}
}
# Validate existing target types
switch ($Target) {
"Group" {
foreach ($Win32AppAssignment in $Win32AppAssignments.value) {
if ($Win32AppAssignment.target.'@odata.type' -eq $DataType) {
if ($Win32AppAssignment.target.groupId -like $GroupID) {
Write-Warning -Message "Win32 app assignment with id '$($Win32AppAssignment.id)' of target type '$($Target)' and GroupID '$($Win32AppAssignment.target.groupId)' already exists, duplicate assignments of this type is not permitted"
$DuplicateAssignmentDetected = $true
}
}
}
}
default {
foreach ($Win32AppAssignment in $Win32AppAssignments.value) {
if ($Win32AppAssignment.target.'@odata.type' -match $TargetType) {
Write-Warning -Message "Win32 app assignment with id '$($Win32AppAssignment.id)' of target type '$($Target)' already exists, duplicate assignments of this type is not permitted"
$DuplicateAssignmentDetected = $true
}
}
}
}
}
else {
Write-Verbose -Message "Detected count of '$($Win32AppAssignmentsCount)', skipping assignment validation for existence of target type: $($Target)"
}
# Handle return value
if ($DuplicateAssignmentDetected -eq $true) {
return $true
}
else {
return $false
}
}
catch [System.Exception] {
Write-Warning -Message "Failed to validate if Win32 app already has an existing assignment target type of '$($Target)'"
}
}
}