Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
<Compile Remove="engine\remoting\commands\ConnectPSSession.cs" />
<Compile Remove="engine\remoting\commands\CustomShellCommands.cs" />
<Compile Remove="engine\remoting\commands\DisconnectPSSession.cs" />
<Compile Remove="engine\remoting\commands\EnterPSHostProcessCommand.cs" />
<Compile Remove="engine\remoting\commands\NewPSSessionOptionCommand.cs" />
<Compile Remove="engine\remoting\commands\ReceivePSSession.cs" />
<Compile Remove="engine\remoting\commands\TestPSSessionConfigurationFile.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5243,9 +5243,6 @@ private static void InitializeCoreCmdletsAndProviders(
#if !UNIX
{"Disable-PSRemoting", new SessionStateCmdletEntry("Disable-PSRemoting", typeof(DisablePSRemotingCommand), helpFile) },
{"Enable-PSRemoting", new SessionStateCmdletEntry("Enable-PSRemoting", typeof(EnablePSRemotingCommand), helpFile) },
{"Get-PSHostProcessInfo", new SessionStateCmdletEntry("Get-PSHostProcessInfo", typeof(GetPSHostProcessInfoCommand), helpFile) },
{"Enter-PSHostProcess", new SessionStateCmdletEntry("Enter-PSHostProcess", typeof(EnterPSHostProcessCommand), helpFile) },
{"Exit-PSHostProcess", new SessionStateCmdletEntry("Exit-PSHostProcess", typeof(ExitPSHostProcessCommand), helpFile) },
{"Disable-PSSessionConfiguration", new SessionStateCmdletEntry("Disable-PSSessionConfiguration", typeof(DisablePSSessionConfigurationCommand), helpFile) },
{"Enable-PSSessionConfiguration", new SessionStateCmdletEntry("Enable-PSSessionConfiguration", typeof(EnablePSSessionConfigurationCommand), helpFile) },
{"Get-PSSessionCapability", new SessionStateCmdletEntry("Get-PSSessionCapability", typeof(GetPSSessionCapabilityCommand), helpFile) },
Expand All @@ -5260,7 +5257,9 @@ private static void InitializeCoreCmdletsAndProviders(
{"Connect-PSSession", new SessionStateCmdletEntry("Connect-PSSession", typeof(ConnectPSSessionCommand), helpFile) },
{"Disconnect-PSSession", new SessionStateCmdletEntry("Disconnect-PSSession", typeof(DisconnectPSSessionCommand), helpFile) },
#endif
{"Enter-PSHostProcess", new SessionStateCmdletEntry("Enter-PSHostProcess", typeof(EnterPSHostProcessCommand), helpFile) },
{"Enter-PSSession", new SessionStateCmdletEntry("Enter-PSSession", typeof(EnterPSSessionCommand), helpFile) },
{"Exit-PSHostProcess", new SessionStateCmdletEntry("Exit-PSHostProcess", typeof(ExitPSHostProcessCommand), helpFile) },
{"Exit-PSSession", new SessionStateCmdletEntry("Exit-PSSession", typeof(ExitPSSessionCommand), helpFile) },
{"Export-ModuleMember", new SessionStateCmdletEntry("Export-ModuleMember", typeof(ExportModuleMemberCommand), helpFile) },
{"ForEach-Object", new SessionStateCmdletEntry("ForEach-Object", typeof(ForEachObjectCommand), helpFile) },
Expand All @@ -5270,6 +5269,7 @@ private static void InitializeCoreCmdletsAndProviders(
{"Get-History", new SessionStateCmdletEntry("Get-History", typeof(GetHistoryCommand), helpFile) },
{"Get-Job", new SessionStateCmdletEntry("Get-Job", typeof(GetJobCommand), helpFile) },
{"Get-Module", new SessionStateCmdletEntry("Get-Module", typeof(GetModuleCommand), helpFile) },
{"Get-PSHostProcessInfo", new SessionStateCmdletEntry("Get-PSHostProcessInfo", typeof(GetPSHostProcessInfoCommand), helpFile) },
{"Get-PSSession", new SessionStateCmdletEntry("Get-PSSession", typeof(GetPSSessionCommand), helpFile) },
{"Import-Module", new SessionStateCmdletEntry("Import-Module", typeof(ImportModuleCommand), helpFile) },
{"Invoke-Command", new SessionStateCmdletEntry("Invoke-Command", typeof(InvokeCommandCommand), helpFile) },
Expand Down
33 changes: 0 additions & 33 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -902,39 +902,6 @@ internal static bool IsAdministrator()
#endif
}

internal static void NativeEnumerateDirectory(string directory, out List<string> directories, out List<string> files)
{
IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
NativeMethods.WIN32_FIND_DATA findData;

files = new List<string>();
directories = new List<string>();

IntPtr findHandle;

findHandle = NativeMethods.FindFirstFile(directory + "\\*", out findData);
if (findHandle != INVALID_HANDLE_VALUE)
{
do
{
if ((findData.dwFileAttributes & NativeMethods.FileAttributes.Directory) != 0)
{
if ((!String.Equals(".", findData.cFileName, StringComparison.OrdinalIgnoreCase)) &&
(!String.Equals("..", findData.cFileName, StringComparison.OrdinalIgnoreCase)))
{
directories.Add(directory + "\\" + findData.cFileName);
}
}
else
{
files.Add(directory + "\\" + findData.cFileName);
}
}
while (NativeMethods.FindNextFile(findHandle, out findData));
NativeMethods.FindClose(findHandle);
}
}

internal static bool IsReservedDeviceName(string destinationPath)
{
#if !UNIX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ private void DoCloseHelper()
// When closing the primary runspace, ensure all other local runspaces are closed.
var closeAllOpenRunspaces = isPrimaryRunspace && haveOpenRunspaces;

// Stop all transcriptions and unitialize AMSI if we're the last runspace to exit or we are exiting the primary runspace.
// Stop all transcriptions and un-initialize AMSI if we're the last runspace to exit or we are exiting the primary runspace.
if (!haveOpenRunspaces)
{
ExecutionContext executionContext = this.GetExecutionContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Remoting;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation.Runspaces;

namespace Microsoft.PowerShell.Commands
{
Expand Down Expand Up @@ -393,7 +394,14 @@ public sealed class GetPSHostProcessInfoCommand : PSCmdlet
private const string ProcessParameterSet = "ProcessParameterSet";
private const string ProcessIdParameterSet = "ProcessIdParameterSet";
private const string ProcessNameParameterSet = "ProcessNameParameterSet";

#if UNIX
// CoreFx uses the system temp path to store the file used for named pipes and is not settable.
// This member is only used by Get-PSHostProcessInfo to know where to look for the named pipe files.
private static readonly string NamedPipePath = Path.GetTempPath();
#else
private const string NamedPipePath = @"\\.\pipe\";
#endif

#endregion

Expand Down Expand Up @@ -524,24 +532,26 @@ internal static IReadOnlyCollection<PSHostProcessInfo> GetAppDomainNamesFromProc
var procAppDomainInfo = new List<PSHostProcessInfo>();

// Get all named pipe 'files' on local machine.
List<string> directories;
List<string> namedPipes;
Utils.NativeEnumerateDirectory(NamedPipePath, out directories, out namedPipes);
List<string> namedPipes = new List<string>();
var namedPipeDirectory = new DirectoryInfo(NamedPipePath);
foreach (var pipeFileInfo in namedPipeDirectory.EnumerateFiles(NamedPipeUtils.NamedPipeNamePrefixSearch))
{
namedPipes.Add(Path.Combine(pipeFileInfo.DirectoryName, pipeFileInfo.Name));
}

// Collect all PowerShell named pipes for given process Ids.
foreach (string namedPipe in namedPipes)
{
int startIndex = namedPipe.IndexOf(NamedPipeUtils.NamedPipeNamePrefix, StringComparison.OrdinalIgnoreCase);
if (startIndex > -1)
{
// This is a PowerShell named pipe. Parse the process Id, AppDomain name, and process name.
int pStartTimeIndex = namedPipe.IndexOf(".", startIndex, StringComparison.OrdinalIgnoreCase);
int pStartTimeIndex = namedPipe.IndexOf('.', startIndex);
if (pStartTimeIndex > -1)
{
int pIdIndex = namedPipe.IndexOf(".", pStartTimeIndex + 1, StringComparison.OrdinalIgnoreCase);
int pIdIndex = namedPipe.IndexOf('.', pStartTimeIndex + 1);
if (pIdIndex > -1)
{
int pAppDomainIndex = namedPipe.IndexOf(".", pIdIndex + 1, StringComparison.OrdinalIgnoreCase);
int pAppDomainIndex = namedPipe.IndexOf('.', pIdIndex + 1);
if (pAppDomainIndex > -1)
{
string idString = namedPipe.Substring(pIdIndex + 1, (pAppDomainIndex - pIdIndex - 1));
Expand All @@ -564,15 +574,47 @@ internal static IReadOnlyCollection<PSHostProcessInfo> GetAppDomainNamesFromProc
if (!found) { continue; }
}
}
else
{
// Process id is not valid so we'll skip
continue;
}

int pNameIndex = namedPipe.IndexOf(".", pAppDomainIndex + 1, StringComparison.OrdinalIgnoreCase);
int pNameIndex = namedPipe.IndexOf('.', pAppDomainIndex + 1);
if (pNameIndex > -1)
{
string appDomainName = namedPipe.Substring(pAppDomainIndex + 1, (pNameIndex - pAppDomainIndex - 1));
string pName = namedPipe.Substring(pNameIndex + 1);

procAppDomainInfo.Add(
new PSHostProcessInfo(pName, id, appDomainName));
Process process = null;

try
{
process = System.Diagnostics.Process.GetProcessById(id);
}
catch (Exception)
{
// Do nothing if the process no longer exists
}

if (process == null)
{
try
{
// If the process is gone, try removing the PSHost named pipe
var pipeFile = new FileInfo(namedPipe);
pipeFile.Delete();
}
catch (Exception)
{
// best effort to cleanup
}
}
else if (process.ProcessName.Equals(pName, StringComparison.Ordinal))
{
// only add if the process name matches
procAppDomainInfo.Add(new PSHostProcessInfo(pName, id, appDomainName));
}
}
}
}
Expand Down Expand Up @@ -661,7 +703,7 @@ internal PSHostProcessInfo(string processName, int processId, string appDomainNa
MainWindowTitle = String.Empty;
try
{
var proc = System.Diagnostics.Process.GetProcessById(processId);
var proc = Process.GetProcessById(processId);
MainWindowTitle = proc.MainWindowTitle ?? string.Empty;
}
catch (ArgumentException) { }
Expand Down
Loading