private Process GetProcessById(int procId) { var process = PSHostProcessUtils.GetProcessById(procId); if (process is null) { ThrowTerminatingError( new ErrorRecord( new PSArgumentException(StringUtil.Format(RemotingErrorIdStrings.EnterPSHostProcessNoProcessFoundWithId, procId)), "EnterPSHostProcessNoProcessFoundWithId", ErrorCategory.InvalidArgument, this) ); } return(process); }
/// <summary> /// Initializes a new instance of the PSHostProcessInfo type. /// </summary> /// <param name="processName">Name of process.</param> /// <param name="processId">Id of process.</param> /// <param name="appDomainName">Name of process AppDomain.</param> /// <param name="pipeNameFilePath">File path of pipe name.</param> internal PSHostProcessInfo( string processName, int processId, string appDomainName, string pipeNameFilePath) { if (string.IsNullOrEmpty(processName)) { throw new PSArgumentNullException(nameof(processName)); } if (string.IsNullOrEmpty(appDomainName)) { throw new PSArgumentNullException(nameof(appDomainName)); } MainWindowTitle = string.Empty; try { var process = PSHostProcessUtils.GetProcessById(processId); MainWindowTitle = process?.MainWindowTitle ?? string.Empty; } catch (ArgumentException) { // Window title is optional. } catch (InvalidOperationException) { // Window title is optional. } this.ProcessName = processName; this.ProcessId = processId; this.AppDomainName = appDomainName; _pipeNameFilePath = pipeNameFilePath; }
/// <summary> /// Returns all named pipe AppDomain names for given process Ids or all PowerShell /// processes if procIds parameter is null. /// PowerShell pipe name example: /// PSHost.130566795082911445.8224.DefaultAppDomain.powershell. /// </summary> /// <param name="procIds">Process Ids or null.</param> /// <returns>Collection of process AppDomain info.</returns> internal static IReadOnlyCollection <PSHostProcessInfo> GetAppDomainNamesFromProcessId(int[] procIds) { var procAppDomainInfo = new List <PSHostProcessInfo>(); // Get all named pipe 'files' on local machine. 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) { int pStartTimeIndex = namedPipe.IndexOf('.', startIndex); if (pStartTimeIndex > -1) { int pIdIndex = namedPipe.IndexOf('.', pStartTimeIndex + 1); if (pIdIndex > -1) { int pAppDomainIndex = namedPipe.IndexOf('.', pIdIndex + 1); if (pAppDomainIndex > -1) { ReadOnlySpan <char> idString = namedPipe.AsSpan(pIdIndex + 1, (pAppDomainIndex - pIdIndex - 1)); int id = -1; if (int.TryParse(idString, out id)) { // Filter on provided proc Ids. if (procIds != null) { bool found = false; foreach (int procId in procIds) { if (id == procId) { found = true; break; } } if (!found) { continue; } } } else { // Process id is not valid so we'll skip continue; } int pNameIndex = namedPipe.IndexOf('.', pAppDomainIndex + 1); if (pNameIndex > -1) { string appDomainName = namedPipe.Substring(pAppDomainIndex + 1, (pNameIndex - pAppDomainIndex - 1)); string pName = namedPipe.Substring(pNameIndex + 1); Process process = null; try { process = PSHostProcessUtils.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 { try { if (process.ProcessName.Equals(pName, StringComparison.Ordinal)) { // only add if the process name matches procAppDomainInfo.Add(new PSHostProcessInfo(pName, id, appDomainName, namedPipe)); } } catch (InvalidOperationException) { // Ignore if process has exited in the mean time. } } } } } } } } if (procAppDomainInfo.Count > 1) { // Sort list by process name. var comparerInfo = CultureInfo.InvariantCulture.CompareInfo; procAppDomainInfo.Sort((firstItem, secondItem) => comparerInfo.Compare(firstItem.ProcessName, secondItem.ProcessName, CompareOptions.IgnoreCase)); } return(new ReadOnlyCollection <PSHostProcessInfo>(procAppDomainInfo)); }