forked from KelvinTegelaar/CIPP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-CIPPW32ScriptApplication.ps1
More file actions
217 lines (187 loc) · 10.4 KB
/
Add-CIPPW32ScriptApplication.ps1
File metadata and controls
217 lines (187 loc) · 10.4 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
function Add-CIPPW32ScriptApplication {
<#
.SYNOPSIS
Adds a Win32 app with PowerShell script installer to Intune using the standard Chocolatey package.
.DESCRIPTION
Creates a Win32 app that uses the standard Chocolatey intunewin package but with custom PowerShell scripts.
Always uploads the same Choco package, but uses user-provided scripts for install/uninstall commands.
.PARAMETER TenantFilter
Tenant ID or domain name for the Graph API call.
.PARAMETER Properties
PSCustomObject containing all Win32 app properties:
- displayName (required): Display name of the app
- description: Description of the app
- publisher: Publisher name
- installScript (required): PowerShell install script content (plaintext)
- uninstallScript: PowerShell uninstall script content (plaintext)
- detectionPath (required): Full path to the file or folder to detect (e.g., 'C:\\Program Files\\MyApp')
- detectionFile: File name to detect (optional, for folder path detection)
- detectionType: 'exists', 'modifiedDate', 'createdDate', 'version', 'sizeInMB' (default: 'exists')
- check32BitOn64System: Boolean, check 32-bit registry/paths on 64-bit systems (default: false)
- runAsAccount: 'system' or 'user' (default: 'system')
- deviceRestartBehavior: 'allow', 'suppress', or 'force' (default: 'suppress')
.EXAMPLE
$Properties = @{
displayName = 'My Script App'
installScript = 'Write-Host "Installing..."'
detectionPath = 'C:\\Program Files\\MyApp'
detectionFile = 'app.exe'
}
Add-CIPPW32ScriptApplication -TenantFilter 'contoso.com' -Properties $Properties
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$TenantFilter,
[Parameter(Mandatory = $true)]
[PSCustomObject]$Properties
)
# Get the standard Chocolatey package location (relative to function app root)
$IntuneWinFile = 'AddChocoApp\IntunePackage.intunewin'
$ChocoXmlFile = 'AddChocoApp\Choco.App.xml'
if (-not (Test-Path $IntuneWinFile)) {
throw "Chocolatey IntunePackage.intunewin not found at: $IntuneWinFile (Current directory: $PWD)"
}
if (-not (Test-Path $ChocoXmlFile)) {
throw "Choco.App.xml not found at: $ChocoXmlFile (Current directory: $PWD)"
}
# Parse the Choco XML to get encryption info. We need a wrapper around the application and this is a tiny intune file, perfect for our purpose.
[xml]$ChocoXml = Get-Content $ChocoXmlFile
$EncryptionInfo = @{
EncryptionKey = $ChocoXml.ApplicationInfo.EncryptionInfo.EncryptionKey
MacKey = $ChocoXml.ApplicationInfo.EncryptionInfo.MacKey
InitializationVector = $ChocoXml.ApplicationInfo.EncryptionInfo.InitializationVector
Mac = $ChocoXml.ApplicationInfo.EncryptionInfo.Mac
ProfileIdentifier = $ChocoXml.ApplicationInfo.EncryptionInfo.ProfileIdentifier
FileDigest = $ChocoXml.ApplicationInfo.EncryptionInfo.FileDigest
FileDigestAlgorithm = $ChocoXml.ApplicationInfo.EncryptionInfo.FileDigestAlgorithm
}
$FileName = $ChocoXml.ApplicationInfo.FileName
$UnencryptedSize = [int64]$ChocoXml.ApplicationInfo.UnencryptedContentSize
# Build detection rules
if ($Properties.detectionPath) {
# Determine if this is a file or folder detection
$DetectionRule = @{
'@odata.type' = '#microsoft.graph.win32LobAppFileSystemDetection'
check32BitOn64System = if ($null -ne $Properties.check32BitOn64System) { [bool]$Properties.check32BitOn64System } else { $false }
detectionType = if ($Properties.detectionType) { $Properties.detectionType } else { 'exists' }
}
if ($Properties.detectionFile) {
# File detection (path + file)
$DetectionRule['path'] = $Properties.detectionPath
$DetectionRule['fileOrFolderName'] = $Properties.detectionFile
} else {
# Folder/File detection (full path)
# Split the path into directory and file/folder name
$PathItem = Split-Path $Properties.detectionPath -Leaf
$ParentPath = Split-Path $Properties.detectionPath -Parent
if ([string]::IsNullOrEmpty($ParentPath)) {
throw "Invalid detection path: $($Properties.detectionPath). Must be a full path."
}
$DetectionRule['path'] = $ParentPath
$DetectionRule['fileOrFolderName'] = $PathItem
}
$DetectionRules = @($DetectionRule)
} else {
# Default detection: Check for a marker file in ProgramData
$DetectionRules = @(
@{
'@odata.type' = '#microsoft.graph.win32LobAppFileSystemDetection'
path = '%ProgramData%\CIPPApps'
fileOrFolderName = "$($Properties.displayName -replace '[^a-zA-Z0-9]', '_').txt"
check32BitOn64System = $false
detectionType = 'exists'
}
)
}
# Build the Win32 app body
$AppBody = @{
'@odata.type' = '#microsoft.graph.win32LobApp'
displayName = $Properties.displayName
description = $Properties.description
publisher = if ($Properties.publisher) { $Properties.publisher } else { 'CIPP' }
fileName = $FileName
setupFilePath = 'N/A'
installCommandLine = 'powershell.exe -ExecutionPolicy Bypass -File install.ps1'
uninstallCommandLine = 'powershell.exe -ExecutionPolicy Bypass -File uninstall.ps1'
minimumSupportedWindowsRelease = '1607'
detectionRules = $DetectionRules
returnCodes = @(
@{ returnCode = 0; type = 'success' }
@{ returnCode = 1707; type = 'success' }
@{ returnCode = 3010; type = 'softReboot' }
@{ returnCode = 1641; type = 'hardReboot' }
@{ returnCode = 1618; type = 'retry' }
)
installExperience = @{
'@odata.type' = 'microsoft.graph.win32LobAppInstallExperience'
runAsAccount = if ($Properties.runAsAccount) { $Properties.runAsAccount } else { 'system' }
deviceRestartBehavior = if ($Properties.deviceRestartBehavior) { $Properties.deviceRestartBehavior } else { 'suppress' }
}
}
# Create the app first
$Baseuri = 'https://graph.microsoft.com/beta/deviceAppManagement/mobileApps'
$NewApp = New-GraphPostRequest -Uri $Baseuri -Body ($AppBody | ConvertTo-Json -Depth 10) -Type POST -tenantid $TenantFilter
Start-Sleep -Milliseconds 200
# Upload the Chocolatey intunewin content
Add-CIPPWin32LobAppContent -AppId $NewApp.id -FilePath $IntuneWinFile -FileName $FileName -UnencryptedSize $UnencryptedSize -EncryptionInfo $EncryptionInfo -TenantFilter $TenantFilter
# Upload PowerShell scripts via the scripts endpoint (newer method)
$InstallScriptId = $null
$UninstallScriptId = $null
if ($Properties.installScript) {
$ReplacedInstallScript = Get-CIPPTextReplacement -Text $Properties.installScript -TenantFilter $TenantFilter
$InstallScriptContent = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($ReplacedInstallScript))
$InstallScriptBody = @{
'@odata.type' = '#microsoft.graph.win32LobAppInstallPowerShellScript'
displayName = 'install.ps1'
enforceSignatureCheck = $false
runAs32Bit = $false
content = $InstallScriptContent
} | ConvertTo-Json
$InstallScriptResponse = New-GraphPostRequest -Uri "$Baseuri/$($NewApp.id)/microsoft.graph.win32LobApp/contentVersions/1/scripts" -Body $InstallScriptBody -Type POST -tenantid $TenantFilter
$InstallScriptId = $InstallScriptResponse.id
# Wait for script to be committed
do {
$ScriptState = New-GraphGetRequest -Uri "$Baseuri/$($NewApp.id)/microsoft.graph.win32LobApp/contentVersions/1/scripts/$InstallScriptId" -tenantid $TenantFilter
if ($ScriptState.state -like '*fail*') {
throw "Failed to commit install script: $($ScriptState.state)"
}
Start-Sleep -Milliseconds 300
} while ($ScriptState.state -eq 'commitPending')
}
if ($Properties.uninstallScript) {
$ReplacedUninstallScript = Get-CIPPTextReplacement -Text $Properties.uninstallScript -TenantFilter $TenantFilter
$UninstallScriptContent = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($ReplacedUninstallScript))
$UninstallScriptBody = @{
'@odata.type' = '#microsoft.graph.win32LobAppUninstallPowerShellScript'
displayName = 'uninstall.ps1'
enforceSignatureCheck = $false
runAs32Bit = $false
content = $UninstallScriptContent
} | ConvertTo-Json
$UninstallScriptResponse = New-GraphPostRequest -Uri "$Baseuri/$($NewApp.id)/microsoft.graph.win32LobApp/contentVersions/1/scripts" -Body $UninstallScriptBody -Type POST -tenantid $TenantFilter
$UninstallScriptId = $UninstallScriptResponse.id
# Wait for script to be committed
do {
$ScriptState = New-GraphGetRequest -Uri "$Baseuri/$($NewApp.id)/microsoft.graph.win32LobApp/contentVersions/1/scripts/$UninstallScriptId" -tenantid $TenantFilter
if ($ScriptState.state -like '*fail*') {
throw "Failed to commit uninstall script: $($ScriptState.state)"
}
Start-Sleep -Milliseconds 300
} while ($ScriptState.state -eq 'commitPending')
}
# Build final commit body with active script references
$CommitBody = @{
'@odata.type' = '#microsoft.graph.win32LobApp'
committedContentVersion = '1'
}
if ($InstallScriptId) {
$CommitBody['activeInstallScript'] = @{ targetId = $InstallScriptId }
}
if ($UninstallScriptId) {
$CommitBody['activeUninstallScript'] = @{ targetId = $UninstallScriptId }
}
# Commit the app with script references
$null = New-GraphPostRequest -Uri "$Baseuri/$($NewApp.id)" -tenantid $TenantFilter -Body ($CommitBody | ConvertTo-Json -Depth 10) -Type PATCH
return $NewApp
}