Skip to content

Commit 8088a1f

Browse files
daxian-dbwSIRMARGIN
authored andcommitted
Allow opt-out of the named-pipe listener using the environment variable POWERSHELL_DIAGNOSTICS_OPTOUT (PowerShell#26086)
1 parent ab65df4 commit 8088a1f

4 files changed

Lines changed: 75 additions & 77 deletions

File tree

src/System.Management.Automation/engine/Utils.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
using System.Threading;
2525
using Microsoft.PowerShell.Commands;
2626
using Microsoft.Win32;
27-
using Microsoft.Win32.SafeHandles;
2827

2928
using TypeTable = System.Management.Automation.Runspaces.TypeTable;
3029

@@ -1548,6 +1547,72 @@ internal static bool IsSessionRestricted(ExecutionContext context)
15481547
}
15491548
return true;
15501549
}
1550+
1551+
/// <summary>
1552+
/// Determine whether the environment variable is set and how.
1553+
/// </summary>
1554+
/// <param name="name">The name of the environment variable.</param>
1555+
/// <param name="defaultValue">If the environment variable is not set, use this as the default value.</param>
1556+
/// <returns>A boolean representing the value of the environment variable.</returns>
1557+
internal static bool GetEnvironmentVariableAsBool(string name, bool defaultValue)
1558+
{
1559+
var str = Environment.GetEnvironmentVariable(name);
1560+
if (string.IsNullOrEmpty(str))
1561+
{
1562+
return defaultValue;
1563+
}
1564+
1565+
var boolStr = str.AsSpan();
1566+
1567+
if (boolStr.Length == 1)
1568+
{
1569+
if (boolStr[0] == '1')
1570+
{
1571+
return true;
1572+
}
1573+
1574+
if (boolStr[0] == '0')
1575+
{
1576+
return false;
1577+
}
1578+
}
1579+
1580+
if (boolStr.Length == 3 &&
1581+
(boolStr[0] == 'y' || boolStr[0] == 'Y') &&
1582+
(boolStr[1] == 'e' || boolStr[1] == 'E') &&
1583+
(boolStr[2] == 's' || boolStr[2] == 'S'))
1584+
{
1585+
return true;
1586+
}
1587+
1588+
if (boolStr.Length == 2 &&
1589+
(boolStr[0] == 'n' || boolStr[0] == 'N') &&
1590+
(boolStr[1] == 'o' || boolStr[1] == 'O'))
1591+
{
1592+
return false;
1593+
}
1594+
1595+
if (boolStr.Length == 4 &&
1596+
(boolStr[0] == 't' || boolStr[0] == 'T') &&
1597+
(boolStr[1] == 'r' || boolStr[1] == 'R') &&
1598+
(boolStr[2] == 'u' || boolStr[2] == 'U') &&
1599+
(boolStr[3] == 'e' || boolStr[3] == 'E'))
1600+
{
1601+
return true;
1602+
}
1603+
1604+
if (boolStr.Length == 5 &&
1605+
(boolStr[0] == 'f' || boolStr[0] == 'F') &&
1606+
(boolStr[1] == 'a' || boolStr[1] == 'A') &&
1607+
(boolStr[2] == 'l' || boolStr[2] == 'L') &&
1608+
(boolStr[3] == 's' || boolStr[3] == 'S') &&
1609+
(boolStr[4] == 'e' || boolStr[4] == 'E'))
1610+
{
1611+
return false;
1612+
}
1613+
1614+
return defaultValue;
1615+
}
15511616
}
15521617
}
15531618

src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System.Security.AccessControl;
1313
using System.Security.Principal;
1414
using System.Threading;
15-
using System.Threading.Tasks;
1615

1716
using Microsoft.Win32.SafeHandles;
1817

@@ -500,13 +499,14 @@ static RemoteSessionNamedPipeServer()
500499
{
501500
s_syncObject = new object();
502501

503-
// All PowerShell instances will start with the named pipe
504-
// and listener created and running.
505-
IPCNamedPipeServerEnabled = true;
506-
507-
CreateIPCNamedPipeServerSingleton();
502+
// Unless opt-out, all PowerShell instances will start with the named-pipe listener created and running.
503+
IPCNamedPipeServerEnabled = !Utils.GetEnvironmentVariableAsBool(name: "POWERSHELL_DIAGNOSTICS_OPTOUT", defaultValue: false);
508504

509-
CreateProcessExitHandler();
505+
if (IPCNamedPipeServerEnabled)
506+
{
507+
CreateIPCNamedPipeServerSingleton();
508+
CreateProcessExitHandler();
509+
}
510510
}
511511

512512
#endregion

src/System.Management.Automation/resources/RemotingErrorIdStrings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ All WinRM sessions connected to PowerShell session configurations, such as Micro
14111411
<value>Multiple processes were found with this name {0}. Use the process Id to specify a single process to enter.</value>
14121412
</data>
14131413
<data name="EnterPSHostProcessNoPowerShell" xml:space="preserve">
1414-
<value>Cannot enter process with Id '{0}' because it has not loaded the PowerShell engine.</value>
1414+
<value>Cannot enter process with Id '{0}' because it has not loaded the PowerShell engine or the named-pipe listener was disabled.</value>
14151415
</data>
14161416
<data name="EnterPSHostProcessNoProcessFoundWithId" xml:space="preserve">
14171417
<value>No process was found with Id: {0}.</value>

src/System.Management.Automation/utils/Telemetry.cs

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Linq;
99
using System.Management.Automation;
1010
using System.Runtime.InteropServices;
11-
using System.Security.AccessControl;
1211
using System.Threading;
1312

1413
using Microsoft.ApplicationInsights;
@@ -177,7 +176,7 @@ public static class ApplicationInsightsTelemetry
177176
static ApplicationInsightsTelemetry()
178177
{
179178
// If we can't send telemetry, there's no reason to do any of this
180-
CanSendTelemetry = !GetEnvironmentVariableAsBool(name: _telemetryOptoutEnvVar, defaultValue: false);
179+
CanSendTelemetry = !Utils.GetEnvironmentVariableAsBool(name: _telemetryOptoutEnvVar, defaultValue: false);
181180
if (CanSendTelemetry)
182181
{
183182
s_sessionId = Guid.NewGuid().ToString();
@@ -643,72 +642,6 @@ static ApplicationInsightsTelemetry()
643642
}
644643
}
645644

646-
/// <summary>
647-
/// Determine whether the environment variable is set and how.
648-
/// </summary>
649-
/// <param name="name">The name of the environment variable.</param>
650-
/// <param name="defaultValue">If the environment variable is not set, use this as the default value.</param>
651-
/// <returns>A boolean representing the value of the environment variable.</returns>
652-
private static bool GetEnvironmentVariableAsBool(string name, bool defaultValue)
653-
{
654-
var str = Environment.GetEnvironmentVariable(name);
655-
if (string.IsNullOrEmpty(str))
656-
{
657-
return defaultValue;
658-
}
659-
660-
var boolStr = str.AsSpan();
661-
662-
if (boolStr.Length == 1)
663-
{
664-
if (boolStr[0] == '1')
665-
{
666-
return true;
667-
}
668-
669-
if (boolStr[0] == '0')
670-
{
671-
return false;
672-
}
673-
}
674-
675-
if (boolStr.Length == 3 &&
676-
(boolStr[0] == 'y' || boolStr[0] == 'Y') &&
677-
(boolStr[1] == 'e' || boolStr[1] == 'E') &&
678-
(boolStr[2] == 's' || boolStr[2] == 'S'))
679-
{
680-
return true;
681-
}
682-
683-
if (boolStr.Length == 2 &&
684-
(boolStr[0] == 'n' || boolStr[0] == 'N') &&
685-
(boolStr[1] == 'o' || boolStr[1] == 'O'))
686-
{
687-
return false;
688-
}
689-
690-
if (boolStr.Length == 4 &&
691-
(boolStr[0] == 't' || boolStr[0] == 'T') &&
692-
(boolStr[1] == 'r' || boolStr[1] == 'R') &&
693-
(boolStr[2] == 'u' || boolStr[2] == 'U') &&
694-
(boolStr[3] == 'e' || boolStr[3] == 'E'))
695-
{
696-
return true;
697-
}
698-
699-
if (boolStr.Length == 5 &&
700-
(boolStr[0] == 'f' || boolStr[0] == 'F') &&
701-
(boolStr[1] == 'a' || boolStr[1] == 'A') &&
702-
(boolStr[2] == 'l' || boolStr[2] == 'L') &&
703-
(boolStr[3] == 's' || boolStr[3] == 'S') &&
704-
(boolStr[4] == 'e' || boolStr[4] == 'E'))
705-
{
706-
return false;
707-
}
708-
709-
return defaultValue;
710-
}
711-
712645
/// <summary>
713646
/// Send module load telemetry as a metric.
714647
/// For modules we send the module name (if allowed), and the version.

0 commit comments

Comments
 (0)