Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@
value="0x3002"
version="1"
/>
<!--Telemetry events-->
<event
channel="C_OPERATIONAL"
level="win:Error"
message="$(string.PS_PROVIDER.event.E_O_TelemetrySettingError.message)"
opcode="Exception"
symbol="TelemetrySettingError"
task="Telemetry"
template="T_TelemetrySettingError"
value="0x3011"
Comment thread
daxian-dbw marked this conversation as resolved.
version="1"
/>
<!--M3P events-->
<event
channel="C_ANALYTIC"
Expand Down Expand Up @@ -2208,17 +2220,41 @@
value="0x6017"
version="1"
/>
<event
channel="C_ANALYTIC"
keywords="AmsiState"
level="win:Verbose"
message="$(string.PS_PROVIDER.event.E_A_AmsiState.message)"
opcode="Method"
symbol="AmsiState"
task="Amsi"
template="T_AmsiState"
value="0x4001"
version="1"
<event
channel="C_ANALYTIC"
keywords="AmsiState"
level="win:Verbose"
message="$(string.PS_PROVIDER.event.E_A_AmsiState.message)"
opcode="Method"
symbol="AmsiState"
task="Amsi"
template="T_AmsiState"
value="0x4001"
version="1"
/>
<event
channel="C_ANALYTIC"
keywords="WDACQuery"
level="win:Verbose"
message="$(string.PS_PROVIDER.event.E_A_WDACQuery.message)"
opcode="Method"
symbol="WDACQuery"
task="WDAC"
template="T_WDACQuery"
value="0x4002"
version="1"
/>
<event
channel="C_ANALYTIC"
keywords="WDACAudit"
level="win:Verbose"
message = "$(string.PS_PROVIDER.event.E_A_WDACAudit.message)"
opcode="Method"
symbol="WDACAudit"
task="WDACAudit"
template="T_WDACAudit"
value="0x4003"
version="1"
/>
</events>
<channels>
Expand Down Expand Up @@ -2409,6 +2445,12 @@
symbol="T_EXPERIMENTALFEATURE"
value="107"
/>
<task
message="$(string.PS_PROVIDER.task.T_Telemetry.message)"
name="Telemetry"
symbol="T_TELEMETRY"
value="108"
/>
<task
message="$(string.PS_PROVIDER.task.T_ScheduledJob.message)"
name="ScheduledJob"
Expand All @@ -2427,11 +2469,23 @@
symbol="T_ISEOperation"
value="120"
/>
<task
message="$(string.PS_PROVIDER.task.T_AmsiState.message)"
name="Amsi"
symbol="T_Amsi"
value="130"
<task
message="$(string.PS_PROVIDER.task.T_AmsiState.message)"
name="Amsi"
symbol="T_Amsi"
value="130"
/>
<task
message="$(string.PS_PROVIDER.task.T_WDACQuery.message)"
name="WDAC"
symbol="T_WDAC"
value="131"
/>
<task
message="$(string.PS_PROVIDER.task.T_WDACAudit.message)"
name="WDACAudit"
symbol="T_WDACAudit"
value="132"
/>
</tasks>
<opcodes>
Expand Down Expand Up @@ -2593,11 +2647,23 @@
name="PSWorkflow"
symbol="K_PSWORKFLOW"
/>
<keyword
mask="0x400"
message="$(string.PS_PROVIDER.keyword.K_AmsiState.message)"
name="AmsiState"
symbol="K_AmsiState"
<keyword
mask="0x400"
message="$(string.PS_PROVIDER.keyword.K_AmsiState.message)"
name="AmsiState"
symbol="K_AmsiState"
/>
<keyword
mask="0x800"
message="$(string.PS_PROVIDER.keyword.K_WDACQuery.message)"
name="WDACQuery"
symbol="K_WDACQuery"
/>
<keyword
mask="0x1000"
message="$(string.PS_PROVIDER.keyword.K_WDACAudit.message)"
name="WDACAudit"
symbol="K_WDACAudit"
/>
</keywords>
<maps>
Expand Down Expand Up @@ -4004,6 +4070,20 @@
name="StackTrace"
/>
</template>
<template tid="T_TelemetrySettingError">
<data
inType="win:UnicodeString"
name="Name"
/>
<data
inType="win:UnicodeString"
name="Message"
/>
<data
inType="win:UnicodeString"
name="StackTrace"
/>
</template>
<template tid="T_TrackingGuid">
<data
inType="win:GUID"
Expand Down Expand Up @@ -4080,16 +4160,48 @@
name="FileName"
/>
</template>
<template tid="T_AmsiState">
<data
inType="win:UnicodeString"
name="Action"
<template tid="T_AmsiState">
<data
inType="win:UnicodeString"
name="Action"
/>
<data
inType="win:UnicodeString"
name="AmsiContext"
<data
inType="win:UnicodeString"
name="AmsiContext"
/>
</template>
</template>
<template tid="T_WDACQuery">
<data
inType="win:UnicodeString"
name="QueryName"
/>
<data
inType="win:UnicodeString"
name="FileName"
/>
<data
inType="win:Int32"
name="QuerySuccess"
/>
<data
inType="win:Int32"
name="QuerySResult"
/>
</template>
<template tid="T_WDACAudit">
<data
inType="win:UnicodeString"
name="Title"
/>
<data
inType="win:UnicodeString"
name="Message"
/>
<data
inType="win:UnicodeString"
name="FullyQualifiedId"
/>
</template>
</templates>
</provider>
</events>
Expand Down Expand Up @@ -5535,6 +5647,14 @@
id="PS_PROVIDER.task.T_ExperimentalFeature.message"
value="PowerShell Experimental Features"
/>
<string
id="PS_PROVIDER.event.E_O_TelemetrySettingError.message"
value="Failed to retrieve diagnostics and feedback setting from Windows.%n Exception: %1 %n Message: %2 %n StackTrace: %3 %n"
/>
<string
id="PS_PROVIDER.task.T_Telemetry.message"
value="PowerShell Telemetry"
/>
<string
id="PS_PROVIDER.task.T_NamedPipe.message"
value="PowerShell Named Pipe IPC"
Expand Down Expand Up @@ -5719,6 +5839,30 @@
id="PS_PROVIDER.event.E_O_REMOTE_NAMEDPIPE_DISCONNECT.message"
value="PowerShell IPC disconnect on process: %1 in AppDomain: %2 for User: %3."
/>
<string
id="PS_PROVIDER.event.E_A_WDACQuery.message"
value="WDAC Query. %n %t Query: %1 %n %t File: %2 %n %t SuccessCode: %3 %n %t ResultCode: %4"
/>
<string
id="PS_PROVIDER.keyword.K_WDACQuery.message"
value="WDAC Query"
/>
<string
id="PS_PROVIDER.task.T_WDACQuery.message"
value="WDAC Query"
/>
<string
id="PS_PROVIDER.event.E_A_WDACAudit.message"
value="WDAC Audit. %n %t Title: %1 %n %t Message: %2 %n %t FullyQualifiedId: %3"
/>
<string
id="PS_PROVIDER.keyword.K_WDACAudit.message"
value="WDAC Audit"
/>
<string
id="PS_PROVIDER.task.T_WDACAudit.message"
value="WDAC Audit"
/>
</stringTable>
</resources>
</localization>
Expand Down
9 changes: 6 additions & 3 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ public static bool IsStaSupported
{
int result = Interop.Windows.CoInitializeEx(IntPtr.Zero, Interop.Windows.COINIT_APARTMENTTHREADED);

// If 0 is returned the thread has been initialized for the first time
// as an STA and thus supported and needs to be uninitialized.
if (result > 0)
// Per COM documentation: Each successful call to CoInitializeEx (including S_FALSE)
// must be balanced by a corresponding call to CoUninitialize.
// - S_OK (0) means we initialized for the first time.
// - S_FALSE (1) means already initialized, but still increments the reference count.
// Both require CoUninitialize to decrement the reference count.
if (result >= 0)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is not related to the feature. I found it when reading the code. The requirement for balancing the call of CoInitializeEx is documented in https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex#remarks. I quote it below:

To uninitialize the COM library gracefully on a thread, each successful call to CoInitialize or CoInitializeEx, including any call that returns S_FALSE, must be balanced by a corresponding call to CoUninitialize.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is not related to the feature. I found it when reading the code. The requirement for balancing the call of CoInitializeEx is documented in (...)

I wonder if anyone is (incorrectly ofc) depending on us having initialized COM. Not suggesting the change be reverted, but worth keeping in mind if it comes up.

{
Interop.Windows.CoUninitialize();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ internal enum PSEventId : int
ExperimentalFeature_InvalidName = 0x3001,
ExperimentalFeature_ReadConfig_Error = 0x3002,

// Windows Diagnostics And Usage Data Settings
Telemetry_Setting_Error = 0x3011,

// Scheduled Jobs
ScheduledJob_Start = 0xD001,
ScheduledJob_Complete = 0xD002,
Expand Down Expand Up @@ -240,6 +243,7 @@ internal enum PSTask : int
ProviderStop = 0x69,
ExecutePipeline = 0x6A,
ExperimentalFeature = 0x6B,
Telemetry = 0x6C,
ScheduledJob = 0x6E,
NamedPipe = 0x6F,
ISEOperation = 0x78,
Expand Down
10 changes: 9 additions & 1 deletion src/System.Management.Automation/utils/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,20 @@ public static class ApplicationInsightsTelemetry
/// </summary>
static ApplicationInsightsTelemetry()
{
// If we can't send telemetry, there's no reason to do any of this
CanSendTelemetry = !Utils.GetEnvironmentVariableAsBool(name: _telemetryOptoutEnvVar, defaultValue: false)
&& Platform.TryDeriveFromCache("telemetry.uuid", out s_uuidPath);

#if !UNIX
if (CanSendTelemetry)
{
// Respect the diagnostics and feedback setting in Windows.
CanSendTelemetry = WindowsDataCollectionSetting.CanCollectDiagnostics(PlatformDataCollectionLevel.Enhanced);
}
#endif

if (!CanSendTelemetry)
{
// Avoid the initialization work if we can't send telemetry.
return;
}

Expand Down
Loading
Loading