Skip to content

Commit b852378

Browse files
authored
Remove the use of Windows PowerShell ETW provider id and update PSDiagnostics module to work for PowerShell 7 (#25590)
1 parent a0cb55c commit b852378

File tree

5 files changed

+31
-44
lines changed

5 files changed

+31
-44
lines changed

src/Modules/Windows/PSDiagnostics/PSDiagnostics.psm1

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
<#
55
PowerShell Diagnostics Module
66
This module contains a set of wrapper scripts that
7-
enable a user to use ETW tracing in Windows
8-
PowerShell.
7+
enable a user to use ETW tracing in PowerShell 7.
98
#>
109

11-
$script:Logman="$env:windir\system32\logman.exe"
12-
$script:wsmanlogfile = "$env:windir\system32\wsmtraces.log"
13-
$script:wsmprovfile = "$env:windir\system32\wsmtraceproviders.txt"
10+
$script:windir = [System.Environment]::GetEnvironmentVariable("windir", [System.EnvironmentVariableTarget]::Machine)
11+
12+
$script:Logman = "${script:windir}\system32\logman.exe"
13+
$script:wsmanlogfile = "${script:windir}\system32\wsmtraces.log"
14+
$script:wsmprovfile = "${script:windir}\system32\wsmtraceproviders.txt"
1415
$script:wsmsession = "wsmlog"
1516
$script:pssession = "PSTrace"
16-
$script:psprovidername="Microsoft-Windows-PowerShell"
17+
$script:psprovidername = "PowerShellCore"
1718
$script:wsmprovidername = "Microsoft-Windows-WinRM"
1819
$script:oplog = "/Operational"
19-
$script:analyticlog="/Analytic"
20-
$script:debuglog="/Debug"
21-
$script:wevtutil="$env:windir\system32\wevtutil.exe"
20+
$script:analyticlog = "/Analytic"
21+
$script:debuglog = "/Debug"
22+
$script:wevtutil = "${script:windir}\system32\wevtutil.exe"
2223
$script:slparam = "sl"
2324
$script:glparam = "gl"
2425

@@ -169,16 +170,15 @@ function Enable-PSWSManCombinedTrace
169170

170171
$provfile = [io.path]::GetTempFilename()
171172

172-
$traceFileName = [string][Guid]::NewGuid()
173173
if ($DoNotOverwriteExistingTrace) {
174174
$fileName = [string][guid]::newguid()
175175
$logfile = $PSHOME + "\\Traces\\PSTrace_$fileName.etl"
176176
} else {
177177
$logfile = $PSHOME + "\\Traces\\PSTrace.etl"
178178
}
179179

180-
"Microsoft-Windows-PowerShell 0 5" | Out-File $provfile -Encoding ascii
181-
"Microsoft-Windows-WinRM 0 5" | Out-File $provfile -Encoding ascii -Append
180+
"$script:psprovidername 0 5" | Out-File $provfile -Encoding ascii
181+
"$script:wsmprovidername 0 5" | Out-File $provfile -Encoding ascii -Append
182182

183183
if (!(Test-Path $PSHOME\Traces))
184184
{
@@ -192,7 +192,7 @@ function Enable-PSWSManCombinedTrace
192192

193193
Start-Trace -SessionName $script:pssession -OutputFilePath $logfile -ProviderFilePath $provfile -ETS
194194

195-
Remove-Item $provfile -Force -ea 0
195+
Remove-Item $provfile -Force -ErrorAction SilentlyContinue
196196
}
197197

198198
function Disable-PSWSManCombinedTrace

src/System.Management.Automation/utils/tracing/EtwActivity.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Diagnostics.Eventing;
88
using System.Diagnostics;
9-
using System.Runtime.InteropServices;
109
using System.Diagnostics.CodeAnalysis;
1110

1211
namespace System.Management.Automation.Tracing

src/System.Management.Automation/utils/tracing/Tracing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Management.Automation.Tracing
1010
/// <summary>
1111
/// Tracer.
1212
/// </summary>
13-
public sealed partial class Tracer : System.Management.Automation.Tracing.EtwActivity
13+
public sealed partial class Tracer : EtwActivity
1414
{
1515
/// <summary>
1616
/// DebugMessage.

src/System.Management.Automation/utils/tracing/TracingGen.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Management.Automation.Tracing
1010
/// <summary>
1111
/// Tracer.
1212
/// </summary>
13-
public sealed partial class Tracer : System.Management.Automation.Tracing.EtwActivity
13+
public sealed partial class Tracer : EtwActivity
1414
{
1515
/// <summary>
1616
/// Critical level.
@@ -37,7 +37,6 @@ public sealed partial class Tracer : System.Management.Automation.Tracing.EtwAct
3737
/// </summary>
3838
public const long KeywordAll = 0xFFFFFFFF;
3939

40-
private static readonly Guid providerId = Guid.Parse("a0c1853b-5c40-4b15-8766-3cf1c58f985a");
4140
private static readonly EventDescriptor WriteTransferEventEvent;
4241
private static readonly EventDescriptor DebugMessageEvent;
4342
private static readonly EventDescriptor M3PAbortingWorkflowExecutionEvent;
@@ -218,17 +217,6 @@ static Tracer()
218217
/// </summary>
219218
public Tracer() : base() { }
220219

221-
/// <summary>
222-
/// Provider Guid.
223-
/// </summary>
224-
protected override Guid ProviderId
225-
{
226-
get
227-
{
228-
return providerId;
229-
}
230-
}
231-
232220
/// <summary>
233221
/// Transfer Event.
234222
/// </summary>

test/powershell/Modules/PSDiagnostics/PSDiagnostics.Tests.ps1

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Describe "PSDiagnostics cmdlets tests." -Tag "CI", "RequireAdminOnWindows" {
99
$PSDefaultParameterValues["it:skip"] = $true
1010
}
1111
else{
12-
$LogSettingBak = Get-LogProperties -Name Microsoft-Windows-PowerShell/$LogType
12+
$LogSettingBak = Get-LogProperties -Name PowerShellCore/$LogType
1313
}
1414
}
1515
AfterAll {
@@ -20,37 +20,37 @@ Describe "PSDiagnostics cmdlets tests." -Tag "CI", "RequireAdminOnWindows" {
2020
}
2121

2222
Context "Test for Enable-PSTrace and Disable-PSTrace cmdlets." {
23-
It "Should enable $LogType logs for Microsoft-Windows-PowerShell." {
24-
[XML]$CurrentSetting = & wevtutil gl Microsoft-Windows-PowerShell/$LogType /f:xml
23+
It "Should enable $LogType logs for PowerShellCore." {
24+
[XML]$CurrentSetting = & wevtutil gl PowerShellCore/$LogType /f:xml
2525
if($CurrentSetting.Channel.Enabled -eq 'true'){
26-
& wevtutil sl Microsoft-Windows-PowerShell/$LogType /e:false /q
26+
& wevtutil sl PowerShellCore/$LogType /e:false /q
2727
}
2828

2929
Enable-PSTrace -Force
3030

31-
[XML]$ExpectedOutput = & wevtutil gl Microsoft-Windows-PowerShell/$LogType /f:xml
31+
[XML]$ExpectedOutput = & wevtutil gl PowerShellCore/$LogType /f:xml
3232

3333
$ExpectedOutput.Channel.enabled | Should -BeExactly 'true'
3434
}
3535

36-
It "Should disable $LogType logs for Microsoft-Windows-PowerShell." {
37-
[XML]$CurrentState = & wevtutil gl Microsoft-Windows-PowerShell/$LogType /f:xml
36+
It "Should disable $LogType logs for PowerShellCore." {
37+
[XML]$CurrentState = & wevtutil gl PowerShellCore/$LogType /f:xml
3838
if($CurrentState.channel.enabled -eq 'false'){
39-
& wevtutil sl Microsoft-Windows-PowerShell/$LogType /e:true /q
39+
& wevtutil sl PowerShellCore/$LogType /e:true /q
4040
}
4141
Disable-PSTrace
4242

43-
[XML]$ExpectedOutput = & wevtutil gl Microsoft-Windows-PowerShell/$LogType /f:xml
43+
[XML]$ExpectedOutput = & wevtutil gl PowerShellCore/$LogType /f:xml
4444

4545
$ExpectedOutput.Channel.enabled | Should -Be 'false'
4646
}
4747
}
4848

4949
Context "Test for Get-LogProperties cmdlet." {
50-
It "Should return properties of $LogType logs for 'Microsoft-Windows-PowerShell'." {
51-
[XML]$ExpectedOutput = wevtutil gl Microsoft-Windows-PowerShell/$LogType /f:xml
50+
It "Should return properties of $LogType logs for 'PowerShellCore'." {
51+
[XML]$ExpectedOutput = wevtutil gl PowerShellCore/$LogType /f:xml
5252

53-
$LogProperty = Get-LogProperties -Name Microsoft-Windows-PowerShell/$LogType
53+
$LogProperty = Get-LogProperties -Name PowerShellCore/$LogType
5454

5555
$LogProperty.Name | Should -Be $ExpectedOutput.channel.Name
5656
$LogProperty.Enabled | Should -Be $ExpectedOutput.channel.Enabled
@@ -67,7 +67,7 @@ Describe "PSDiagnostics cmdlets tests." -Tag "CI", "RequireAdminOnWindows" {
6767
Context "Test for Set-LogProperties cmdlet." {
6868
BeforeAll {
6969
if ($IsWindows) {
70-
[XML]$WevtUtilBefore = wevtutil gl Microsoft-Windows-PowerShell/$LogType /f:xml
70+
[XML]$WevtUtilBefore = wevtutil gl PowerShellCore/$LogType /f:xml
7171
$LogPropertyToSet = [Microsoft.PowerShell.Diagnostics.LogDetails]::new($WevtUtilBefore.channel.Name,
7272
[bool]::Parse($WevtUtilBefore.channel.Enabled),
7373
$LogType,
@@ -78,12 +78,12 @@ Describe "PSDiagnostics cmdlets tests." -Tag "CI", "RequireAdminOnWindows" {
7878
}
7979
}
8080

81-
It "Should invert AutoBackup setting of $LogType logs for 'Microsoft-Windows-PowerShell'." {
81+
It "Should invert AutoBackup setting of $LogType logs for 'PowerShellCore'." {
8282
$LogPropertyToSet.AutoBackup = -not $LogPropertyToSet.AutoBackup
8383
Set-LogProperties -LogDetails $LogPropertyToSet -Force
8484

85-
[XML]$ExpectedOutput = & wevtutil gl Microsoft-Windows-PowerShell/$LogType /f:xml
86-
(Get-LogProperties -Name Microsoft-Windows-PowerShell/$LogType).AutoBackup | Should -Be ([bool]::Parse($ExpectedOutput.Channel.Logging.AutoBackup))
85+
[XML]$ExpectedOutput = & wevtutil gl PowerShellCore/$LogType /f:xml
86+
(Get-LogProperties -Name PowerShellCore/$LogType).AutoBackup | Should -Be ([bool]::Parse($ExpectedOutput.Channel.Logging.AutoBackup))
8787
}
8888

8989
It "Should throw exception for invalid LogName." {

0 commit comments

Comments
 (0)