-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExport-InstalledApplications.ps1
More file actions
287 lines (227 loc) · 8.18 KB
/
Export-InstalledApplications.ps1
File metadata and controls
287 lines (227 loc) · 8.18 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
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
<#
.SYNOPSIS
Exports Action1 endpoint's software to CSV.
.DESCRIPTION
Retrieves endpoints, their group membership, and installed software.
Supports filtering by endpoint group name.
Exports result to CSV file.
Documentation: https://github.com/Action1Corp/PSAction1/
Use Action1 Roadmap system (https://roadmap.action1.com/) to submit feedback or enhancement requests.
WARNING: Carefully study the provided scripts and components before using them. Test in your non-production lab first.
Action1 Public Repository Material
Subject to TERMS_OF_USE.md (https://github.com/Action1Corp/PSAction1/blob/main/TERMS_OF_USE.md)
Provided AS IS
Use at your own risk
Review and test before production deployment
© Action1 Corporation
.PARAMETER ApiKey
Action1 API key for authentication.
.PARAMETER Secret
Action1 API secret.
.PARAMETER Region
Action1 region.
.PARAMETER Org
Action1 Organization Name or Organization Id.
.PARAMETER CSVExportPath
Output CSV file path.
.PARAMETER GroupNames
Optional comma-separeted list of Action1 endpoint group names to filter.
.NOTES
Minimum PowerShell version: 5.1
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$ApiKey,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Secret,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateSet('NorthAmerica', 'Europe', 'Australia')]
[string]$Region,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Org,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$CSVExportPath,
[Parameter(Mandatory = $false)]
[string[]]$GroupNames
)
Install-Module -Name PSAction1 -Force
Set-Action1Credentials -APIKey $ApiKey -Secret $Secret
Set-Action1Region -Region $Region
function Get-OrganizationId {
param([string]$IdOrName)
$organizations = Get-Action1 -Query Organizations
$organization = $organizations | Where-Object { $_.name -eq $IdOrName -or $_.id -eq $IdOrName }
if (-not $organization) {
Write-Error "Organization with Name or Id $($IdOrName) not found."
exit 1
}
return $organization.id
}
Set-Action1DefaultOrg -Org_ID (Get-OrganizationId -IdOrName $Org)
# Get endpoint groups
$groupProcessing = $true
$endpointGroups = Get-Action1 -Query EndpointGroups
if (-not $endpointGroups) {
Write-Error "Failed to retrieve endpoint groups."
exit 1
}
# Check if there are any groups in the organization
if ($endpointGroups.GetType().Name -eq "PSCustomObject" -and $endpointGroups.type -eq "ResultPage") {
Write-Warning "No Endpoint Group found in organization: $($Org)"
$groupProcessing = $false
}
# Validate GroupNames entered by user with existing ones in the Org
$filterByGroups = $false
$allowedGroupIds = @{}
if ($GroupNames -and $GroupNames.Count -gt 0 -and $groupProcessing) {
$invalidGroupNames = @()
$groupNameToIdMap = @{}
foreach ($endpointGroup in $endpointGroups) {
$groupNameToIdMap[$endpointGroup.name] = $endpointGroup.id
}
foreach ($groupName in $GroupNames) {
if (-not $groupNameToIdMap.ContainsKey($groupName)) {
$invalidGroupNames += $groupName
}
else {
$allowedGroupIds[$groupNameToIdMap[$groupName]] = $true
}
}
if ($invalidGroupNames.Count -gt 0) {
Write-Error "Invalid group name(s): $($invalidGroupNames -join ', ')"
exit 1
}
$filterByGroups = $true
}
# Get endpoints
$endpoints = Get-Action1 -Query Endpoints
# Check if there are any Endpoints in the org
if (-not $endpoints -or $endpoints.GetType().Name -eq "PSCustomObject" -and $endpoints.type -eq "ResultPage") {
Write-Error "No endpoints retrieved for organization: $($Org)."
exit 1
}
# Filter endpoints by Groups
$endpointsToProcess = @()
if ($filterByGroups) {
$endpointsToProcess = foreach ($endpoint in $endpoints) {
$endpointGroupsMembership = $endpoint.group_membership
if (-not $endpointGroupsMembership) {
continue
}
foreach ($endpointGroup in $endpointGroupsMembership) {
if ($allowedGroupIds.ContainsKey($endpointGroup.id)) {
$endpoint
break
}
}
}
}
else {
$endpointsToProcess = [System.Array]$endpoints
}
# Prepare CSV
if (Test-Path $CSVExportPath) {
Remove-Item $CSVExportPath -Force
}
$script:isFirstWrite = $true
function Write-CsvRow {
param($Object)
if ($script:isFirstWrite) {
$Object | Export-Csv -Path $CSVExportPath -NoTypeInformation -Encoding UTF8
$script:isFirstWrite = $false
}
else {
$Object | Export-Csv -Path $CSVExportPath -NoTypeInformation -Encoding UTF8 -Append
}
}
# endpoint processing progress bar
$totalEndpointsToProcessCount = $endpointsToProcess.Count
$processingEndpointIndex = 0
if ($totalEndpointsToProcessCount -lt 1){
Write-Warning "There are no Endpoints to process in organization: $($Org) with GroupName(s) filter: $($GroupNames -join ', ')"
exit 1
}
foreach ($endpoint in $endpointsToProcess) {
$processingEndpointIndex++
Write-Progress `
-Activity "Exporting endpoint software..." `
-Status "$processingEndpointIndex/ $totalEndpointsToProcessCount ($($endpoint.name))" `
-PercentComplete (($processingEndpointIndex/ $totalEndpointsToProcessCount) * 100)
# Normalize last_seen
$endpointLastSeen = $endpoint.last_seen
if ($endpointLastSeen -eq "Now") {
$endpointLastSeen = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
}
# Handle group membership
$endpointGroupsMembership = $endpoint.group_membership
if (-not $endpointGroupsMembership -or $endpointGroupsMembership.Count -eq 0) {
$endpointGroupsMembership = @(
[PSCustomObject]@{
name = $null
id = $null
}
)
}
# Get installed apps
try {
$endpointAppsResponse = Get-Action1 -Query EndpointApps -Id $endpoint.id
$installedApps = if ($endpointAppsResponse -and $endpointAppsResponse.Fields) {
$endpointAppsResponse.Fields
}
else {
@()
}
}
catch {
Write-Warning "Failed to get apps for endpoint '$($endpoint.name)': $_"
$installedApps = @()
}
foreach ($endpointGroup in $endpointGroupsMembership) {
if ($installedApps.Count -eq 0) {
Write-CsvRow ([PSCustomObject]@{
"Endpoint Name" = $endpoint.name
"Endpoint Id" = $endpoint.id
"Endpoint User" = $endpoint.user
"Endpoint Last seen" = $endpointLastSeen
"Endpoint Group Name" = $endpointGroup.name
"Endpoint Group Id" = $endpointGroup.id
"Name" = $null
"Version" = $null
"Install Location" = $null
"Installed For" = $null
"Install Date" = $null
"Vendor" = $null
"Install Type" = $null
"Update Status" = $null
"Platform" = $null
})
continue
}
foreach ($app in $installedApps) {
Write-CsvRow ([PSCustomObject]@{
"Endpoint Name" = $endpoint.name
"Endpoint Id" = $endpoint.id
"Endpoint User" = $endpoint.user
"Endpoint Last seen" = $endpointLastSeen
"Endpoint Group Name" = $endpointGroup.name
"Endpoint Group Id" = $endpointGroup.id
"Name" = $app.Name
"Version" = $app.Version
"Install Location" = $app.'Install Location'
"Installed For" = $app.'Installed For'
"Install Date" = $app.'Install Date'
"Vendor" = $app.Vendor
"Install Type" = $app.'Install Type'
"Update Status" = $app.'Update Status'
"Platform" = $app.Platform
})
}
}
}
Write-Host "Export completed: $CSVExportPath"