Skip to content

Commit ebbaf1e

Browse files
committed
- Fixed a bug when a .NET version was not installed and the registry key could not be found
- Fixed a bug when only a single core was selected to be tested
1 parent 022880e commit ebbaf1e

File tree

2 files changed

+233
-72
lines changed

2 files changed

+233
-72
lines changed

helpers/add-eventlog-source.ps1

+167-42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ param(
1414
)
1515

1616

17+
Set-StrictMode -Version 3.0
18+
19+
1720

1821
<#
1922
.DESCRIPTION
@@ -41,75 +44,197 @@ function Exit-WithErrorCode {
4144
[Void]
4245
#>
4346
function Wait-ForUserInput {
44-
Write-Output 'Press any key to continue...'
47+
Write-Host
48+
Write-Host 'Press any key to continue...'
4549
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
4650
}
4751

4852

4953

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)
5068

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+
}
5278

5379

54-
if (!$areWeAdmin) {
55-
Write-Output 'We don''t have admin privileges, aborting!'
5680

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
60100
}
61101

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+
}
64109

65110

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+
}
72143

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+
}
76155

77-
# Indicate that the process should be elevated
78-
$newProcess.Verb = "runas"
79156

80-
# Start the new process
81-
[System.Diagnostics.Process]::Start($newProcess)
82157

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)
90166

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 $_
102167

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
103172
if (!$shouldBeAdmin) {
104173
Wait-ForUserInput
105174
}
106175

176+
# Set the exit code = error code
107177
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+
#>
108200
}
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+
}
109231

110232

111-
if (!$shouldBeAdmin) {
112-
Wait-ForUserInput
233+
if (!$shouldBeAdmin) {
234+
Wait-ForUserInput
235+
}
113236
}
114237
}
115238

239+
240+
Start-Main

0 commit comments

Comments
 (0)