-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-iOSApplications.ps1
More file actions
254 lines (224 loc) · 7.66 KB
/
Add-iOSApplications.ps1
File metadata and controls
254 lines (224 loc) · 7.66 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
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
$ErrorActionPreference = "Stop"
# ================================
# Information
# ================================
# This script will add iOS Store Applications to Intune using metadata from the iOS store
# The Applications will then be assigned to the Security Group as specified within $TargetGroupName & $AssignmentIntent
# I need to look at incorporating a switch for uninstallOnDeviceRemoval
# James Vincent - January 2026
# ================================
# Configuration
# ================================
# Enter your App Registration credentials as per;
# https://jamesvincent.co.uk/2025/01/16/connecting-to-microsoft-graph-api-through-powershell-via-an-app-registration/
$AppId = ''
$TenantId = ''
$Secret = ''
# Security Group to which the apps should be assigned
$TargetGroupName = "Managed iOS Devices - James"
# Assignment Intent, this can be required|available|uninstall|availableWithoutEnrollment
$AssignmentIntent = "required"
# AppStore location (gb|us|es|fr etc)
$Country = "gb"
# List of Apps to publish and assign
$iOSApps = @(
"Microsoft Outlook",
"Microsoft Teams"
)
# Logging location
$LogFile = "$env:TEMP\iOSAppDeployment.log"
# ================================
# NOTHING TO EDIT BELOW HERE
# ================================
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# ================================
# Logging Function
# ================================
function Write-Log {
param (
[string]$Message,
[string]$Level = "INFO"
)
$Timestamp = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
$LogMessage = "[$Timestamp][$Level] $Message"
Write-Host $LogMessage -ForegroundColor ($Level -eq "ERROR" ? "Red" : ($Level -eq "WARN" ? "Yellow" : "Green"))
Add-Content -Path $LogFile -Value $LogMessage
}
Write-Log "Starting iOS app deployment script"
# ================================
# Connect to Microsoft Graph
# ================================
try {
$SecureSecret = ConvertTo-SecureString -String $Secret -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AppId, $SecureSecret
Connect-MgGraph -TenantId $TenantId -ClientSecretCredential $Cred -NoWelcome
Write-Log "Connected to Microsoft Graph"
} catch {
Write-Log "Failed to connect to Microsoft Graph: $_" "ERROR"
throw
}
# ================================
# Get Target Group
# ================================
try {
$TargetGroup = Get-MgGroup -Filter "displayName eq '$TargetGroupName'"
if (-not $TargetGroup) { throw "Security group '$TargetGroupName' not found." }
Write-Log "Using target group: $($TargetGroup.DisplayName)"
} catch {
Write-Log $_ "ERROR"
throw
}
# ================================
# Create and Assign Apps
# ================================
foreach ($AppName in $iOSApps) {
Write-Log "Processing app: $AppName"
# ================================
# Lookup App Store metadata
# ================================
$EncodedName = [System.Web.HttpUtility]::UrlEncode($AppName)
$Uri = "https://itunes.apple.com/search?term=$EncodedName&country=$Country&entity=software&limit=1"
try {
$Response = Invoke-RestMethod -Uri $Uri -Method Get -ErrorAction Stop
if ($Response.resultCount -eq 0) {
Write-Log "No results found for '$AppName'" "WARN"
continue
}
$App = $Response.results[0]
Write-Log "Found App Store metadata for '$($App.trackName)'"
} catch {
Write-Log "Failed to query App Store for '$AppName': $_" "ERROR"
continue
}
# ================================
# Download App Icon and Prepare
# ================================
$RandomFileName = [System.IO.Path]::GetRandomFileName() -replace '\.', ''
$TempFile = Join-Path $env:TEMP "$RandomFileName.tmp"
$PngPath = Join-Path $env:TEMP "$RandomFileName.png"
Invoke-WebRequest -Uri $App.artworkUrl512 -OutFile $TempFile -UseBasicParsing
Write-Log "Downloaded app icon to $TempFile"
Add-Type -AssemblyName System.Drawing
$Image = [System.Drawing.Image]::FromFile($TempFile)
$PngPath = "$env:TEMP\$($App.trackName)-AppIcon-512.png"
$Image.Save($PngPath, [System.Drawing.Imaging.ImageFormat]::Png)
$Image.Dispose()
Write-Log "Converted logo to PNG: $PngPath"
$LogoBytes = [System.IO.File]::ReadAllBytes($PngPath)
# ================================
# Create iOS App in Intune
# ================================
$AppBody = @{
"@odata.type" = "#microsoft.graph.iosStoreApp"
displayName = $App.trackName
description = $App.description
publisher = $App.sellerName
appStoreUrl = $App.trackViewUrl
bundleId = $App.bundleId
developer = $App.artistName
notes = $App.releaseNotes
applicableDeviceType = @{
iPad = $true
iPhoneAndIPod = $true
}
minimumSupportedOperatingSystem = @{
v10_0 = $true
}
largeIcon = @{
"@odata.type" = "microsoft.graph.mimeContent"
type = "image/png"
value = $LogoBytes
}
}
try {
$CreatedApp = New-MgDeviceAppManagementMobileApp -BodyParameter $AppBody
Write-Log "Created iOS App: $($App.trackName)"
} catch {
Write-Log "Failed to create app '$($App.trackName)': $_" "ERROR"
continue
}
# ================================
# Wait for App to be published - can't assign an app that doesn't exist
# ================================
$MaxTimeSec = 60
$IntervalSec = 15
$Elapsed = 0
$AppPublished = $false
while ($Elapsed -lt $MaxTimeSec) {
try {
$Result = Get-MgDeviceAppManagementMobileApp -MobileAppId $CreatedApp.Id
Write-Log "Checking if '$($App.trackName)' is published..."
Start-Sleep 5
if ($Result) {
Write-Log "'$($App.trackName)' is published. Assigning to group..."
$AppPublished = $true
break
}
} catch {
Write-Log "Error checking publication status: $_" "WARN"
}
Start-Sleep -Seconds $IntervalSec
$Elapsed += $IntervalSec
}
if (-not $AppPublished) {
Write-Log "'$($App.trackName)' not published in time." "ERROR"
continue
}
# ================================
# Assign App to Target Group
# ================================
try {
$AssignmentBody = @{
mobileAppAssignments = @(
@{
"@odata.type" = "#microsoft.graph.mobileAppAssignment"
intent = "$AssignmentIntent"
target = @{
"@odata.type" = "#microsoft.graph.groupAssignmentTarget"
groupId = $TargetGroup.Id
}
}
)
}
Invoke-MgGraphRequest -Method POST `
-Uri "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps/$($CreatedApp.Id)/assign" `
-Body ($AssignmentBody | ConvertTo-Json -Depth 10) `
-ContentType "application/json"
Write-Log "Assigned '$($App.trackName)' to '$($TargetGroup.DisplayName)'"
} catch {
Write-Log "Failed to assign app '$($App.trackName)': $_" "ERROR"
continue
}
Write-Log "Processing completed for app: $AppName"
}
Write-Log "All applications processed successfully" "INFO"