diff --git a/.globalconfig b/.globalconfig index 32bea9725fd..cfa6110e7b6 100644 --- a/.globalconfig +++ b/.globalconfig @@ -323,7 +323,7 @@ dotnet_diagnostic.CA1835.severity = suggestion dotnet_diagnostic.CA1836.severity = warning # CA1837: Use 'Environment.ProcessId' -dotnet_diagnostic.CA1837.severity = suggestion +dotnet_diagnostic.CA1837.severity = warning # CA1838: Avoid 'StringBuilder' parameters for P/Invokes dotnet_diagnostic.CA1838.severity = silent diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index d3daea0be1b..32e33d59d6a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -974,7 +974,7 @@ protected override void ProcessRecord() } // It cannot wait on itself - if (process.Id.Equals(System.Diagnostics.Process.GetCurrentProcess().Id)) + if (process.Id.Equals(Environment.ProcessId)) { WriteNonTerminatingError(process, null, ProcessResources.WaitOnItself, "WaitOnItself", ErrorCategory.ObjectNotFound); continue; @@ -1214,7 +1214,7 @@ protected override void ProcessRecord() try { - if (Process.GetCurrentProcess().Id == SafeGetProcessId(process)) + if (Environment.ProcessId == SafeGetProcessId(process)) { _shouldKillCurrentProcess = true; continue; diff --git a/src/System.Management.Automation/engine/InformationRecord.cs b/src/System.Management.Automation/engine/InformationRecord.cs index 6794a2b3ad7..29d317e2c02 100644 --- a/src/System.Management.Automation/engine/InformationRecord.cs +++ b/src/System.Management.Automation/engine/InformationRecord.cs @@ -137,7 +137,7 @@ public uint ProcessId { if (!this._processId.HasValue) { - this._processId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id; + this._processId = (uint)Environment.ProcessId; } return this._processId.Value; diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index 89b1ea38c4b..caaaa17a9ac 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -511,7 +511,7 @@ private void LogTranscriptHeader(System.Management.Automation.Remoting.PSSenderI Environment.MachineName, Environment.OSVersion.VersionString, string.Join(" ", Environment.GetCommandLineArgs()), - System.Diagnostics.Process.GetCurrentProcess().Id, + Environment.ProcessId, versionInfoFooter.ToString().TrimEnd()); } diff --git a/src/System.Management.Automation/engine/remoting/commands/EnterPSHostProcessCommand.cs b/src/System.Management.Automation/engine/remoting/commands/EnterPSHostProcessCommand.cs index d7b7ca51782..454cc10efd0 100644 --- a/src/System.Management.Automation/engine/remoting/commands/EnterPSHostProcessCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/EnterPSHostProcessCommand.cs @@ -377,7 +377,7 @@ private Process GetProcessByName(string name) private void VerifyProcess(Process process) { - if (process.Id == Process.GetCurrentProcess().Id) + if (process.Id == Environment.ProcessId) { ThrowTerminatingError( new ErrorRecord( diff --git a/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs b/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs index 8310aae2166..d50b7989b66 100644 --- a/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs +++ b/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs @@ -744,7 +744,7 @@ private void WaitForConnection() [SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Runtime.InteropServices.SafeHandle.DangerousGetHandle")] private void ProcessListeningThread(object state) { - string processId = System.Diagnostics.Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture); + string processId = Environment.ProcessId.ToString(CultureInfo.InvariantCulture); string appDomainName = NamedPipeUtils.GetCurrentAppDomainName(); // Logging. diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index 8571c9999b6..187ed387bc8 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -78,7 +78,7 @@ internal static ProcessModule GetMainModule(Process targetProcess) // Cache of the current process' parentId private static int? s_currentParentProcessId; - private static readonly int s_currentProcessId = Process.GetCurrentProcess().Id; + private static readonly int s_currentProcessId = Environment.ProcessId; /// /// Retrieve the parent process of a process.