14
14
)
15
15
16
16
17
+ Set-StrictMode - Version 3.0
18
+
19
+
17
20
18
21
<#
19
22
. DESCRIPTION
@@ -41,75 +44,197 @@ function Exit-WithErrorCode {
41
44
[Void]
42
45
#>
43
46
function Wait-ForUserInput {
44
- Write-Output ' Press any key to continue...'
47
+ Write-Host
48
+ Write-Host ' Press any key to continue...'
45
49
$null = $Host.UI.RawUI.ReadKey (" NoEcho,IncludeKeyDown" )
46
50
}
47
51
48
52
49
53
54
+ <#
55
+ . DESCRIPTION
56
+ Ask the user if we should also add the Custom View in the Event Log
57
+ . OUTPUTS
58
+ [Bool]
59
+ #>
60
+ function Request-ToAddCustomView {
61
+ $title = ' Do you also want to add a Custom View in the Windows Event Log?'
62
+ $question = ' This Custom View will make it easier to find the CoreCycler entries in the Event Log.' + [Environment ]::NewLine + ' '
63
+ $choices = @ (
64
+ [System.Management.Automation.Host.ChoiceDescription ]::new(' &Yes' , ' Add the Custom View in the Windows Event Log' )
65
+ [System.Management.Automation.Host.ChoiceDescription ]::new(' &No' , ' Do not add the Custom View' )
66
+ )
67
+ $decision = $Host.UI.PromptForChoice ($title , $question , $choices , 0 )
50
68
51
- $areWeAdmin = ([Security.Principal.WindowsPrincipal ][Security.Principal.WindowsIdentity ]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole ]::Administrator)
69
+ if ($decision -eq 0 ) {
70
+ return $true
71
+ }
72
+ else {
73
+ return $false
74
+ }
75
+
76
+ return $false
77
+ }
52
78
53
79
54
- if (! $areWeAdmin ) {
55
- Write-Output ' We don'' t have admin privileges, aborting!'
56
80
57
- # If the flag is set, don't wait for a keypress, as we're not in a new window
58
- if (! $shouldBeAdmin ) {
59
- Wait-ForUserInput
81
+ <#
82
+ . DESCRIPTION
83
+ Create the XML document for the Event Log Custom View
84
+ . OUTPUTS
85
+ [Void]
86
+ #>
87
+ function Add-XmlFileForCustomView {
88
+ $path = $Env: ProgramData + ' \Microsoft\Event Viewer\Views\'
89
+ $fileName = ' CoreCyclerEventLogCustomView.xml'
90
+ $filePath = Join-Path - Path $path - ChildPath $fileName
91
+
92
+ Write-Host
93
+
94
+ # File path doesn't exist
95
+ if (! (Test-Path $path - PathType Container)) {
96
+ Write-Host ' The path to put the config file for the Custom View into doesn'' t exist!' - ForegroundColor Red
97
+ Write-Host (' (' + $path + ' )' ) - ForegroundColor Red
98
+ Write-Host ' Not trying to create the file' - ForegroundColor Red
99
+ return
60
100
}
61
101
62
- # Set the exit code = error code
63
- Exit-WithErrorCode 1
102
+ # Skip if the file already exists
103
+ if (Test-Path $filePath - PathType Leaf) {
104
+ Write-Host ' The config file for the Custom View already exists!' - ForegroundColor Red
105
+ Write-Host (' (' + $filePath + ' )' ) - ForegroundColor Red
106
+ Write-Host ' Not adding it again' - ForegroundColor Red
107
+ return
108
+ }
64
109
65
110
66
- # Not sure if this will work with the rest of the main script
67
- <#
68
- Write-Output 'Trying to re-open in a new window with admin privileges'
69
-
70
- # Create a new process object that starts PowerShell
71
- $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell"
111
+ $xmlText = @'
112
+ <ViewerConfig>
113
+ <QueryConfig>
114
+ <QueryParams>
115
+ <Simple>
116
+ <Channel>Application</Channel>
117
+ <RelativeTimeInfo>0</RelativeTimeInfo>
118
+ <Source>CoreCycler</Source>
119
+ <BySource>False</BySource>
120
+ </Simple>
121
+ </QueryParams>
122
+ <QueryNode>
123
+ <Name>CoreCycler</Name>
124
+ <QueryList>
125
+ <Query Id="0" Path="Application">
126
+ <Select Path="Application">*[System[Provider[@Name='CoreCycler']]]</Select>
127
+ </Query>
128
+ </QueryList>
129
+ </QueryNode>
130
+ </QueryConfig>
131
+ </ViewerConfig>
132
+ '@
133
+
134
+
135
+ $createdFile = New-Item - Path $path - Name $fileName - ItemType File - Force
136
+
137
+ # The file wasn't created
138
+ if (! (Test-Path $filePath - PathType Leaf)) {
139
+ Write-Host ' Could not create the config file for the Custom View!' - ForegroundColor Red
140
+ Write-Host (' (' + $filePath + ' )' ) - ForegroundColor Red
141
+ return
142
+ }
72
143
73
- # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
74
- #$newProcess.Arguments = "& '" + $Script:MyInvocation.MyCommand.Path + "'"
75
- $newProcess.Arguments = "& '" + $MyInvocation.MyCommand.Path + "'"
144
+ try {
145
+ [System.IO.File ]::WriteAllLines($createdFile , $xmlText )
146
+ Write-Host ' Custom View added to the Windows Event Log!' - ForegroundColor Green
147
+ Write-Host
148
+ }
149
+ catch {
150
+ Write-Host ' Could not add the XML to the config file for the Custom View!' - ForegroundColor Red
151
+ Write-Host (' (' + $filePath + ' )' ) - ForegroundColor Red
152
+ return
153
+ }
154
+ }
76
155
77
- # Indicate that the process should be elevated
78
- $newProcess.Verb = "runas"
79
156
80
- # Start the new process
81
- [System.Diagnostics.Process]::Start($newProcess)
82
157
83
- # Exit from the current, unelevated, process
84
- Exit
85
- #>
86
- }
87
- else {
88
- try {
89
- Write-Output ' Creating the Event Log Source "CoreCycler"'
158
+ <#
159
+ . DESCRIPTION
160
+ The main functionality
161
+ . OUTPUTS
162
+ [Void]
163
+ #>
164
+ function Start-Main {
165
+ $areWeAdmin = ([Security.Principal.WindowsPrincipal ][Security.Principal.WindowsIdentity ]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole ]::Administrator)
90
166
91
- if (-not [System.Diagnostics.EventLog ]::SourceExists(' CoreCycler' )) {
92
- [System.Diagnostics.EventLog ]::CreateEventSource(' CoreCycler' , ' Application' )
93
- Write-Output ' Successfully created the Source'
94
- }
95
- else {
96
- Write-Output ' The Source "CoreCycler" already existed, continuing'
97
- }
98
- }
99
- catch {
100
- Write-Output ' Some error has happened:'
101
- Write-Output $_
102
167
168
+ if (! $areWeAdmin ) {
169
+ Write-Host ' We don'' t have admin privileges, aborting!' - ForegroundColor Red
170
+
171
+ # If the flag is set, don't wait for a keypress, as we're not in a new window
103
172
if (! $shouldBeAdmin ) {
104
173
Wait-ForUserInput
105
174
}
106
175
176
+ # Set the exit code = error code
107
177
Exit-WithErrorCode 1
178
+
179
+
180
+ # Not sure if this will work with the rest of the main script
181
+ <#
182
+ Write-Host 'Trying to re-open in a new window with admin privileges'
183
+
184
+ # Create a new process object that starts PowerShell
185
+ $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell"
186
+
187
+ # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
188
+ #$newProcess.Arguments = "& '" + $Script:MyInvocation.MyCommand.Path + "'"
189
+ $newProcess.Arguments = "& '" + $MyInvocation.MyCommand.Path + "'"
190
+
191
+ # Indicate that the process should be elevated
192
+ $newProcess.Verb = "runas"
193
+
194
+ # Start the new process
195
+ [System.Diagnostics.Process]::Start($newProcess)
196
+
197
+ # Exit from the current, unelevated, process
198
+ Exit
199
+ #>
108
200
}
201
+ else {
202
+ try {
203
+ Write-Host ' Creating the Windows Event Log Source "CoreCycler"...'
204
+ Write-Host
205
+
206
+ if (-not [System.Diagnostics.EventLog ]::SourceExists(' CoreCycler' )) {
207
+ [System.Diagnostics.EventLog ]::CreateEventSource(' CoreCycler' , ' Application' )
208
+ Write-Host ' Successfully created the Event Log Source!' - ForegroundColor Green
209
+ Write-Host
210
+
211
+ $addCustomView = Request-ToAddCustomView
212
+
213
+ if ($addCustomView ) {
214
+ Add-XmlFileForCustomView
215
+ }
216
+ }
217
+ else {
218
+ Write-Host ' The Source "CoreCycler" already existed, continuing'
219
+ }
220
+ }
221
+ catch {
222
+ Write-Host ' Some error has happened!' - ForegroundColor Red
223
+ Write-Host $_ - ForegroundColor Red
224
+
225
+ if (! $shouldBeAdmin ) {
226
+ Wait-ForUserInput
227
+ }
228
+
229
+ Exit-WithErrorCode 1
230
+ }
109
231
110
232
111
- if (! $shouldBeAdmin ) {
112
- Wait-ForUserInput
233
+ if (! $shouldBeAdmin ) {
234
+ Wait-ForUserInput
235
+ }
113
236
}
114
237
}
115
238
239
+
240
+ Start-Main
0 commit comments