-
Notifications
You must be signed in to change notification settings - Fork 0
/
installed-apps-report.ps1
325 lines (248 loc) · 10.5 KB
/
installed-apps-report.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<#
.SYNOPSIS
Generates a report of installed applications on a Windows system and uploads it to an S3 bucket.
.DESCRIPTION
This script performs the following tasks:
1. Retrieves system details such as FQDN, computer name, and OS version.
2. Creates an event log source if it doesn't already exist.
3. Defines a function to get installed applications from the registry.
4. Determines the OS architecture and fetches applications accordingly.
5. Formats the application data and outputs it to the console, a CSV file, or a GridView.
6. Imports required PowerShell modules for AWS S3 operations.
7. Generates the installed applications report and logs the event.
8. Uploads the generated report to an S3 bucket.
9. Logs the success or failure of the upload operation.
.FUNCTION Get-InstalledApplication
Retrieves installed applications from the registry.
.FUNCTION Import-PSModule
Imports the required PowerShell modules if not already imported.
.EXAMPLE
To generate and upload a report of installed applications:
Run the script without any parameters.
.EXAMPLE
To view the installed applications in a GridView:
Modify the 'Get-InstalledApplication' call to use '-OutputType 'GridView''.
.EXAMPLE
To output the installed applications to the console:
Modify the 'Get-InstalledApplication' call to use '-OutputType 'Console''.
.NOTES
- Ensure the required AWS Tools PowerShell modules are installed and available.
- Ensure the appropriate permissions are set for the S3 bucket access.
Author : Ivica Agatunovic
WebSite: https://github.com/ivicaagatunovic
Linkedin: www.linkedin.com/in/ivica-agatunovic-96090024
#>
$fqdn = ([System.Net.Dns]::GetHostByName(($env:computerName))).hostname
$computername = $env:computerName
$osversion = (Get-CimInstance win32_OperatingSystem).Caption
$EndpointUrl ="https:\\s3bucketendpoint"
$reportKey = $env:mapsreportkey
$reportSecret = $env:mapsreportsecret
$reportpath = "C:\MAPS\REPORTS\$fqdn.csv"
$eventidreport = 1111
$eventidupload = 2222
$eventiduploaderror = 3333
$modules = ("AWS.Tools.Common","AWS.Tools.S3")
New-EventLog -LogName 'Application' -Source 'MAPS' -ErrorAction SilentlyContinue
Function Get-InstalledApplication
{
Param(
[Parameter(Mandatory=$true)]
[String[]]$OutputType,
[string[]]$outpath
)
#Registry Hives
$Object =@()
# Since I am doing a report only for Microsoft products, I filter out only Micriosfr apps in the array
$includeArray = @("*Office*","*SQL*","*Microsoft*","*Windows*","*Visio*","*Project*","*Visual*","*System*","*Agent*","*Access*","*Autoroute*","*MapPoint*","*AX2012*","*AX2009*","*CRM*","*Configuration Manager*","*Dynamics*","*Word*","*PowerPoint*","*Excel*","*Access*","*Operations*","*Power Bi*","SharePoint","*Team*","*OneNote*","*Skype*")
[long]$HIVE_HKROOT = 2147483648
[long]$HIVE_HKCU = 2147483649
[long]$HIVE_HKLM = 2147483650
[long]$HIVE_HKU = 2147483651
[long]$HIVE_HKCC = 2147483653
[long]$HIVE_HKDD = 2147483654
$Query = Get-WmiObject -query "Select AddressWidth, DataWidth,Architecture from Win32_Processor"
foreach ($i in $Query)
{
If($i.AddressWidth -eq 64){
$OSArch='64-bit'
}
Else{
$OSArch='32-bit'
}
}
Switch ($OSArch)
{
"64-bit"{
$RegProv = GWMI -Namespace "root\Default" -list | where{$_.Name -eq "StdRegProv"}
$Hive = $HIVE_HKLM
$RegKey_64BitApps_64BitOS = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
$RegKey_32BitApps_64BitOS = "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$RegKey_32BitApps_32BitOS = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
#############################################################################
# Get SubKey names
$SubKeys = $RegProv.EnumKey($HIVE, $RegKey_64BitApps_64BitOS)
# Make Sure No Error when Reading Registry
if ($SubKeys.ReturnValue -eq 0)
{ # Loop through all returned subkeys
ForEach ($Name in $SubKeys.sNames)
{
$SubKey = "$RegKey_64BitApps_64BitOS\$Name"
$ValueName = "DisplayName"
$ValuesReturned = $RegProv.GetStringValue($Hive, $SubKey, $ValueName)
$AppName = $ValuesReturned.sValue
$Version = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayVersion")).sValue
$Publisher = ($RegProv.GetStringValue($Hive, $SubKey, "Publisher")).sValue
if($AppName.length -gt "0"){
Foreach($include in $includeArray) {
if ($AppName -like $include) {
$Object += New-Object PSObject -Property @{
ServerName = $fqdn;
OSversion = $osversion;
Application = $AppName;
Architecture = "64-BIT";
Version = $Version;
Publisher= $Publisher;
}
}
}
}
}}
#############################################################################
$SubKeys = $RegProv.EnumKey($HIVE, $RegKey_32BitApps_64BitOS)
# Make Sure No Error when Reading Registry
if ($SubKeys.ReturnValue -eq 0)
{
# Loop Through All Returned SubKEys
ForEach ($Name in $SubKeys.sNames)
{
$SubKey = "$RegKey_32BitApps_64BitOS\$Name"
$ValueName = "DisplayName"
$ValuesReturned = $RegProv.GetStringValue($Hive, $SubKey, $ValueName)
$AppName = $ValuesReturned.sValue
$Version = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayVersion")).sValue
$Publisher = ($RegProv.GetStringValue($Hive, $SubKey, "Publisher")).sValue
if($AppName.length -gt "0"){
Foreach($include in $includeArray) {
if ($AppName -like $include) {
$Object += New-Object PSObject -Property @{
ServerName = $fqdn;
OSversion = $osversion;
Application = $AppName;
Architecture = "32-BIT";
Version = $Version;
Publisher= $Publisher;
}
}
}
}
}
}
} #End of 64 Bit
######################################################################################
###########################################################################################
"32-bit"{
$RegProv = GWMI -Namespace "root\Default" -list | where{$_.Name -eq "StdRegProv"}
$Hive = $HIVE_HKLM
$RegKey_32BitApps_32BitOS = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
#############################################################################
# Get SubKey names
$SubKeys = $RegProv.EnumKey($HIVE, $RegKey_32BitApps_32BitOS)
# Make Sure No Error when Reading Registry
if ($SubKeys.ReturnValue -eq 0)
{ # Loop Through All Returned SubKEys
ForEach ($Name in $SubKeys.sNames)
{
$SubKey = "$RegKey_32BitApps_32BitOS\$Name"
$ValueName = "DisplayName"
$ValuesReturned = $RegProv.GetStringValue($Hive, $SubKey, $ValueName)
$AppName = $ValuesReturned.sValue
$Version = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayVersion")).sValue
$Publisher = ($RegProv.GetStringValue($Hive, $SubKey, "Publisher")).sValue
if($AppName.length -gt "0"){
$Object += New-Object PSObject -Property @{
ServerName = $fqdn;
OSversion = $osversion;
Application = $AppName;
Architecture = "32-BIT";
Version = $Version;
Publisher= $Publisher;
}
}
}}
}#End of 32 bit
} # End of Switch
#}
#$AppsReport
$column1 = @{expression="ServerName"; width=15; label="Name"; alignment="left"}
$column2 = @{expression="OSversion"; width=15; label="Name"; alignment="left"}
$column3 = @{expression="Architecture"; width=10; label="32/64 Bit"; alignment="left"}
$column4 = @{expression="Application"; width=80; label="Application"; alignment="left"}
$column5 = @{expression="Version"; width=15; label="Version"; alignment="left"}
$column6 = @{expression="Publisher"; width=30; label="Publisher"; alignment="left"}
if ($outputType -eq "Console")
{
"#"*80
"Installed Software Application Report"
"Number of Installed Application count : $($object.count)"
"Generated $(get-date)"
"Generated from $(gc env:computername)"
"#"*80
$object |Format-Table $column1, $column2, $column3 ,$column4, $column5, $column6
}
elseif ($OutputType -eq "GridView")
{
$object|Out-GridView
}
elseif ($OutputType -eq "CSV")
{
[string]$outFile = "$outpath\temp.csv"
New-Item -ItemType file $outfile -Force
$object | export-csv -path $outfile -NoTypeInformation
Get-Content "$outfile" | Get-Unique > "C:\MAPS\REPORTS\$fqdn.csv"
Remove-Item "$outfile" -Force -ErrorAction SilentlyContinue
}
else
{
write-host " Invalid Output Type $OutputType"
}
}
function Import-PSModule ($m) {
# If module is imported say that and do nothing
if (Get-Module | Where-Object {$_.Name -eq $m}) {
write-host "Module $m is already imported."
}
else {
# If module is not imported, but available on disk then import
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
Import-Module $m -Force
write-host "INFO: Module available on disk but not loaded. Importing $m ... "
}
else {
# If module is not imported, not available on disk, but is in online gallery then install and import
Install-Module $m
write-host "INFO: Installing PS module $m ... "
Import-Module $m -Force
write-host "INFO: Importing PS module $m ... "
}
}
}
foreach ($module in $modules) {
Import-PSModule $module
}
Get-InstalledApplication -OutputType 'CSV' -outpath 'C:\MAPS\REPORTS'
Write-EventLog -LogName 'Application' -Source 'MAPS' -EntryType Information -EventID $eventidreport -Message "Installed Applications report generated on $fqdn"
#Upload report to Cloudian S3 bucket
if (Test-Path $reportpath){
Write-Host "INFO :: Uploading report to -> S3"
import-module AWS.Tools.Common ; import-module AWS.Tools.S3
Set-AWSCredential -AccessKey $reportKey -SecretKey $reportSecret
Write-S3Object -BucketName 'appsreport' -File $reportpath -EndpointUrl $endpointUrl
Write-Host "SUCCES :: succesfully uploaded $reportpath -> S3"
Write-EventLog -LogName 'Application' -Source 'MAPS' -EntryType Information -EventID $eventidupload -Message "Report uploaded to S3"
#clean up after upload
#remove-item $report
}else{
Write-Host "WARNING :: no report to upload..."
Write-EventLog -LogName 'Application' -Source 'MAPS' -EntryType Information -EventID $eventiduploaderror -Message "WARNING :: no report to upload..."
}