-1

I have a process (testxx) running on my Database. Every time a user connects with the database the process will start separate session for. All processes include a private environment variable(Client-Nr) with different values. I want to invoke a particular process with the particular private environment variable. I used the following command:

Let's say there are 5x "testxx"-processes. I want to invoke the one process which has the value "Client-Two" in the private environment variable "Client-Nr".

Therefore, I use the following code:

get-process -name "testxx" | where-object {$env:Client-Nr -eq "client-Two"}

It didn't invoke the process I needed. I checked with the following command, if PowerShell recognize the private environment variable:

(get-process -Name "testxx").StartInfo.EnvironmentVariables

PowerShell didn't recognized this private environment variable. However, if I open "Process Hacker", choose the specific "testxx" process, I see the private environment variable "client-Nr" with that particular "Client-nr" value. How can I invoke this kind of private environment variable via PowerShell?

Keeran
  • 302
  • 1
  • 15
  • I got blocked, just because, the post that was mentioned , didn't help me or what ? – Keeran Sep 07 '21 at 12:31
  • @keeran Your question is not entirely clear. Are you trying to find the process that has a `FirstName` env variable set in its environment block? – Mathias R. Jessen Sep 07 '21 at 12:35
  • Sorry for the inconvenience beforehand. I didn't know, what i was dealing with. pls, don't block the account, just because the questiong isn't clear. I can't explain it well, if i don't understand what i am dealing with. I have edited my post once again. Hope, you guys understand my problem now. – Keeran Sep 08 '21 at 11:58
  • @Olaf thx for your time. I think, my problem is sth different. Sorry for the inconvenience. Could you take a look at my post again. I have edited it. – Keeran Sep 08 '21 at 12:01
  • @keeran You can use environment variables in a machine or a user context. But both ways would need the PowerShell session you want to use these variables in to be started after you created or set the environment variables. Another way would be to use either a kind of settings file or a registry value. Both would not require to have the PowerShell session started after the first PowerShell session. ;-) – Olaf Sep 08 '21 at 12:14
  • @Olaf It didn't work :( I set the env-variable($env:FirstName = "Sam") first using a PS session and then startet another PS session and tried to GET the first PS session using the command "get-process -name powershell | where-object {$env:FirstName -eq "Sam"}. But i didn't work. – Keeran Sep 08 '21 at 12:21
  • @Olaf the command i used is: (get-process -id "PID").StartInfo.EnvironmentVariables. It didn't show my private environmental variable – Keeran Sep 08 '21 at 12:32
  • I think we have a missunderstanding. I thought you want to read the environment variable and not the process what set up this environment variable. There might be a more sophisticated way but what should work actually would be to manipulate the MainWindowTitle of the first PowerShell session and query the processes for the one with the MainWindowTitle you just set up from the second session. With `$Host.UI.RawUI.WindowTitle = 'Whatever'` you can set the Window Title of the current console. – Olaf Sep 08 '21 at 12:33
  • @Olaf speaking of WindowTitle, there is coming the next big problem. It is not really a PS session that owns this private env-variable. I just gave an example with PS session. I actually have a different process, which works with Database. This Process owns this private env-variable. for every user that connect with the databse, there will be a process with the same private env-variable but with different values. I am going to use a PS-Script to invoke a particular process with a particular env-variable value. how can i call this process and then get the private env-variable ? – Keeran Sep 08 '21 at 12:45
  • @Olaf I tested your "windowTitle" methode. It didn't work for me. It shows all the env-variables, beside my private env-variable. – Keeran Sep 08 '21 at 12:54
  • Sorry .... but ALL THIS should have been and still should be in your question - not to the comments. Update it!! And it will be impossible to help you if you change the conditions again and again. – Olaf Sep 08 '21 at 14:07
  • @Olaf sorry for that. I edited my question as you wished. Can you start sth with this ? – Keeran Sep 10 '21 at 11:31

1 Answers1

0

I think this will be to long for a comment but actually it is meant as one. ;-)

I'm still not really sure if I got what you want. But if it is what I think it is you may go the wrong way. I think PowerShell (Windows) does not have the concept of private environment variables only belonging to one single running process. I think the environment variables you can get with .StartInfo.EnvironmentVariables from the object of the type System.Diagnostics.Process are the environment variables inherited by the process when it started. So they will not change over time the process runs.

You would need an attribute what's - manipulated from the inside of the process - visible to the outside of the process. For manually started consoles this could be the MainWindowTitle.

You may test this. I created a file GUID-Title_90Sec.ps1 with the content:

$Host.UI.RawUI.WindowTitle = $(New-Guid)
Start-Sleep -Seconds 90

Now I ran this a few times:

Start-Process Powershell -ArgumentList '-NoProfile', '-File .\GUID-Title_90Sec.ps1' 

And with ...

Get-Process -Name PowerShell | 
    Select-Object -Property Name,MainWindowTitle

... you could list all of those processes what's looking something like this:

Name       MainWindowTitle
----       ---------------
powershell ParentProcess
powershell ff37b8b6-f791-4d2b-8dfd-8c0dc0a8bc96
powershell fe06d061-784f-441b-8cd4-0a2d3ec040c5
powershell 9f954fb5-cda8-4f2a-98fe-391c59c51ab2
powershell 095a8019-08d5-4461-ab3c-d6f0f2f485cb
powershell

I just doubt that this would work with processes started by a database engine because they probably will not have a MainWindowTitle. Sorry. ;-)

Edit:

Instead of trying to identify the processes afterwards you could keep track of the processes you start. When you save the process info the moment you start it in a variable you can use it later on. With ...

$PowerShellInstance = Start-Process Powershell -PassThru

you start a new PowerShell session. With ...

$PowerShellInstance

... you have all information about this process. For example ...

$PowerShellInstance.Id 

... shows the process id. Could this be helpful? ;-)

Olaf
  • 4,690
  • 2
  • 15
  • 23