Powershell: Extend Process with Environment Property

Created on 13 Mar 2017  路  10Comments  路  Source: PowerShell/PowerShell

Add a CodeMethod to extend System.Diagnostics.Process with and Environment property, returning a hashtable of the Environment variables for the process.

Area-Cmdlets-Management Issue-Enhancement Up-for-Grabs

Most helpful comment

@vexx32 Thanks, that solved the puzzle between the methods and the members of the Process object from the *-Process cmdlets.

I think I have enough to get started working on it

All 10 comments

3322 On all supported platforms, the implementation for getting one is almost identical to getting the other. (i.e. cmdline vs env)

It would probably need to be a hashtable instance with a case sensitive comparer for OSX/Linux, as their environments' keys are CS.

@powercode I am interested on helping with this issue. First time contributor here

After a dive in the codebase I have a couple of points:

  • System.Diagnostics.Process is part of the .Net Core framework, which means we have to use extensions methods, am I correct that the class to modify should be ProcessCodeMethods?
  • If so, should this extension method be part of Process objects retrieved by the _-Process_ cmdlets? Because the _GetParentProcess_ method, which is in the ProcessCodeMethods file, is not part of the Process object retrieved by Get-Process cmldet (I locally confirmed it).

@renehernandez Yeah, that looks about the right place to put it.

I believe that those methods aren't applied to the Process objects as-is; instead, CodeProperty methods are applied as ETS methods to the objects when they're retrieved by the Get-Process cmdlet.

See here for how it's done with applying the Parent property to process objects, referencing the internal helper method:

https://github.com/PowerShell/PowerShell/blob/f31750c0d122b0d33976f16802454f0ca58418a5/src/System.Management.Automation/engine/Types_Ps1Xml.cs#L278-L279

@vexx32 Thanks, that solved the puzzle between the methods and the members of the Process object from the *-Process cmdlets.

I think I have enough to get started working on it

For what I have been reading, there is no standard implementation in .Net to get the runtime environment variables for a given process.

As far as existing properties in the Process class, the StartInfo property provides Environment and EnvironmentVariables properties which may store custom envvar applied before starting the process, but may be null otherwise.

The best line of development that I could find was to leverage the ideas and example from gapotchenko.

Any ideas?

@SteveL-MSFT @iSazonov do either of you fine folks have any experience in this area? A quick read through that blog post looks like it might require some p/invoke on Windows at the very least.

Perhaps we ought to check with the .NET Core folks to see if they have plans to fix this issue in the Process class itself. A quick search on the CoreFX repo seems to indicate it hasn't been raised as an issue there yet, if a search of the obvious keywords are any indication.

I think it should be implemented in CoreFX at first. Feel free to contribute there.

I agree with @iSazonov that I would start by raising the issue in corefx

Was this page helpful?
0 / 5 - 0 ratings