-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJavaUsageTracking.ps1
More file actions
240 lines (209 loc) · 10.2 KB
/
JavaUsageTracking.ps1
File metadata and controls
240 lines (209 loc) · 10.2 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
$LogFile = (Get-ItemProperty -Path "HKLM:\Software\Microsoft\SMS\Client\Configuration\Client Properties" -Name "Local SMS Path").'Local SMS Path' + "Logs\CM_JavaUsageLogging.log"
$LoggingEnable = $True
$UTLogFileName = ".java_usage_cm"
$OverwriteUT = $false
##########################################################################################################
Function Log-ScriptEvent {
#Thank you Ian Farr https://gallery.technet.microsoft.com/scriptcenter/Log-ScriptEvent-Function-ea238b85
#Define and validate parameters
[CmdletBinding()]
Param(
#The information to log
[parameter(Mandatory=$True)]
[String]$Value,
#The severity (1 - Information, 2- Warning, 3 - Error)
[parameter(Mandatory=$True)]
[ValidateRange(1,3)]
[Single]$Severity
)
#Obtain UTC offset
$DateTime = New-Object -ComObject WbemScripting.SWbemDateTime
$DateTime.SetVarDate($(Get-Date))
$UtcValue = $DateTime.Value
$UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21)
#Create the line to be logged
$LogLine = "<![LOG[$Value]LOG]!>" +`
"<time=`"$(Get-Date -Format HH:mm:ss.fff)$($UtcOffset)`" " +`
"date=`"$(Get-Date -Format M-d-yyyy)`" " +`
"component=`"Java Compliance`" " +`
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " +`
"type=`"$Severity`" " +`
"thread=`"$([Threading.Thread]::CurrentThread.ManagedThreadId)`" " +`
"file=`"`">"
#Write the line to the passed log file
Add-Content -Path $LogFile -Value $LogLine
}
function Create-CMJavaUsageTracking {
<#
.SYNOPSIS
Create Java usagetracking WMI Class
#>
process {
try {
$newClass = New-Object System.Management.ManagementClass("root\cimv2", [String]::Empty, $null);
$newClass["__CLASS"] = "CM_JavaUsageTracking";
$newClass.Properties.Add("User", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("Type", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("DateTime", [System.Management.CimType]::String, $false)
$newClass.Properties["DateTime"].Qualifiers.Add("Key", $true)
$newClass.Properties.Add("HostIP", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("Command", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("JREPath", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("JavaVer", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("JREVer", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("JavaVen", [System.Management.CimType]::String, $false)
$newClass.Properties.Add("JVMVen", [System.Management.CimType]::String, $false)
$newClass.Put()
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "CM_JavaUsageTracking class creation complete." -Severity 1}
} CATCH {
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "CM_JavaUsageTracking class creation error." -Severity 3}
}
}
}
Function Create-UsageTrackingProps {
<#
.SYNOPSIS
Create Java usagetracking.properties config file in specified path.
#>
[CmdletBinding()]
Param(
#The information to log
[parameter(Mandatory=$True)]
[String]$UTPath
)
process {
$utprops=@'
# UsageTracker template properties file.
# Copy to JRE/lib/management/usagetracker.properties and edit,
# For more info reference http://docs.oracle.com/javacomponents/usage-tracker/overview/index.html
# Settings for logging to a file:
# Use forward slashes (/) because backslash is an escape character in a
# properties file.
com.oracle.usagetracker.logToFile = ${user.home}/.java_usage_cm
# (Optional) Specify a file size limit in bytes:
com.oracle.usagetracker.logFileMaxSize = 10000000
# Additional options:
# com.oracle.usagetracker.verbose = true
com.oracle.usagetracker.separator = ^
com.oracle.usagetracker.innerQuote = '
com.oracle.usagetracker.quote = "
'@
$utprops | Out-File -Encoding "UTF8" $UTPath
}
}
##########################################################################################################
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Starting Java logging discovery." -Severity 1}
#Set Variables
$header = "Type","DateTime","HostIP","Command","JREPath","JavaVer","JREVer","JavaVen","JVMVen","OS","Arch","OSVer","JVMArg","ClassPath"
$DataSet = @()
#Enable Java logging by enumerating the JREs from the registry
$Keys = Get-ChildItem "HKLM:\Software\WOW6432Node\JavaSoft\Java Runtime Environment"
$Keys += Get-ChildItem "HKLM:\Software\JavaSoft\Java Runtime Environment"
$JREs = $Keys | Foreach-Object {Get-ItemProperty $_.PsPath }
ForEach ($JRE in $JREs) {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Interogating JRE path $($JRE.JavaHome)" -Severity 1
}
$JREPath = test-path "$($JRE.JavaHome)\lib\management"
if ($JREPath) {
$UTProps = test-path "$($JRE.JavaHome)\lib\management\usagetracker.properties"
if (-Not $UTProps -or $OverwriteUT) {
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Creating $($JRE.JavaHome)\lib\management\usagetracker.properties" -Severity 1}
Create-UsageTrackingProps -UTPath "$($JRE.JavaHome)\lib\management\usagetracker.properties"
} else {
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "$($JRE.JavaHome)\lib\management\usagetracker.properties exists" -Severity 1}
}
}
}
#Enumerate user profile folders from WMI
try {
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Gather user profile paths." -Severity 1}
$users = Get-WMIObject win32_userprofile | Select-Object LocalPath
} Catch {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Error gather user profile paths." -Severity 3
}
Exit 5150
}
#Check each returned folder for a java uasge log.
Foreach ($user in $users) {
Write-Host ($($user.LocalPath) -split '\\')[-1].ToString()
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Checking for $($user.LocalPath)\$($UTLogFileName)" -Severity 1}
$path = test-path "$($user.LocalPath)\$($UTLogFileName)"
if ($path) {
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Found $($user.LocalPath)\$($UTLogFileName), attempting to load data." -Severity 1}
Try {
$Data = import-csv "$($user.LocalPath)\$($UTLogFileName)" -Delimiter '^' -Header $header
$data | ForEach-Object {
# Thanks https://stackoverflow.com/questions/17180955/trim-object-contents-in-csv-import
$_.PSObject.Properties | Foreach-Object {$_.Value = $_.Value.Trim()}
}
$Dataset += $Data | Select-Object @{Name="User";Expression={($($user.LocalPath) -split '\\')[-1].ToString()}},Type,DateTime,HostIP,@{Name="Command";Expression={if($_.Command -like 'http*'){($_.Command -split ': ')[0].ToString() } else {($_.Command -split ':')[0]}}},JREPath,JavaVer,JREVer,JavaVen,JVMVen
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Parsing $($user.LocalPath)\$($UTLogFileName)" -Severity 1}
} Catch {
Write-Host "Wowzzers" #Wicked error here
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Error parsing $($user.LocalPath)\$($UTLogFileName)" -Severity 3}
Exit 5150
}
}
}
IF ($LoggingEnable -eq $true) {Log-ScriptEvent -Value "Completed data discovery, writing data to WMI" -Severity 1}
#Check for WMI Class
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Verifying WMI Class exists.." -Severity 1
}
$WMICheck = Get-WmiObject -Class 'CM_JavaUsageTracking' -List -Namespace 'root\cimv2'
If (($null -ne $WMICheck) -eq $false) {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "CM_JavaUsageTracking class not found, creating class." -Severity 1
}
Create-CMJavaUsageTracking
#Validate created class
$WMIVerify = Get-WmiObject -Class "CM_JavaUsageTracking" -List -Namespace 'root\cimv2'
If (($null -ne $WMIVerify) -eq $false) {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Error creating class." -Severity 3
}
Exit 5150
} else {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Verified CM_JavaUsageTracking class exists." -Severity 1
}
}
} else {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Verified CM_JavaUsageTracking class exists." -Severity 1
}
}
#Check if logged instances are in WMI
ForEach ($Record in $DataSet) {
$Instance = Get-WmiObject -Query "select Type from CM_JavaUsageTracking where DateTime='$($Record.DateTime)' and Command='$($Record.Command)'"
if (($null -ne $instance) -eq $false) {
#Add record when not found
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Adding record to WMI datastore..." -Severity 1
}
$Arguments = @{User ="$($Record.User)";`
Type = "$($Record.Type)";`
DateTime = "$($Record.DateTime)";`
HostIP = "$($Record.HostIP)";`
Command = "$($Record.Command)";`
JREPath = "$($Record.JREPath)";`
JavaVer = "$($Record.JavaVer)";`
JREVer = "$($Record.JREVer)";`
JavaVen = "$($Record.JavaVen)";`
JVMVen = "$($Record.JVMVen)";}
Try {
Set-WmiInstance -Class CM_JavaUsageTracking -argument $Arguments
} Catch {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Error inserting record." -Severity 3
}
Exit 5150
}
} else {
IF ($LoggingEnable -eq $true) {
Log-ScriptEvent -Value "Record already inserted" -Severity 1
}
}
}