1

I'm writing a Java program which will pull out the process related logs for an application. For this I need to get the Java processes created in a specific location. I need to know how to get this information on a Windows box using a single command or a combination of commands that can be passed in a single line.

I did a bit of studying and ended up with PowerShell as an option. I get the result using two separate PowerShell commands:

Powershell
Get-Process java| where {$_.path -like 'D:\ptc\Windchill_11.0\Java\jre\bin\java.exe'}

enter image description here

But when I combine the two, no results are obtained:

Powershell ; Get-Process java ^| where {$_.path -like 'D:/ptc/Windchill_11.0/Java/jre/bin/java.exe'}

enter image description here

Can anyone help me to combine the two, or is there any other option other than PowerShell?

0

1 Answer 1

0

OK, this means you are very new to PowerShell, and not yet invested the needed time to get ramped up on it, so, making some guesses. That is just does not really help you.

Not ramping up will just lead to unnecessary confusion, frustration, errors, bad habits, misconceptions, etc. So, my suggest to get some ramp up is so you can decrease / avoid such experiences. Still, with any learning, some guessing / experimenting is going to happen, because that is the nature of the beast. ;-}

Don't mix java syntax with PowerShell code. There are many reserved words, variables in PowerShell and specific ways you have to construct code to run scripts, commands and .exes.

The semicolon in PowerShell does not make that line a one liner (pipeline one liner). That is two separate independent commands / code blocks on the same line. It's a statement terminator.

You must be already in a PowerShell session before you can run PowerShell commands.

So, this...

Get-Process java| where {$_.path -like "D:\ptc\Windchill_11.0\Java\jre\bin\java.exe"}

… works, because you apparently already started PowerShell, and manually typed this command.

This..

 Powershell ; Get-Process java ^| where {$_.path -like 'D:/ptc/Windchill_11.0/Java/jre/bin/java.exe'} enter image description here

… fails, because you are not in a PowerShell session before executing a PowerShell command.

See: PowerShell.exe Command-Line Help

# EXAMPLES

# Create a new PowerShell session and load a saved console file
PowerShell -PSConsoleFile sqlsnapin.psc1

# Create a new PowerShell V2 session with text input, XML output, and no logo
PowerShell -Version 2.0 -NoLogo -InputFormat text -OutputFormat XML

# Execute a PowerShell Command in a session
PowerShell -Command "Get-EventLog -LogName security"

# Run a script block in a session
PowerShell -Command {Get-EventLog -LogName security}

# An alternate way to run a command in a new session
PowerShell -Command "& {Get-EventLog -LogName security}"

# To use the -EncodedCommand parameter:
$command = "dir 'c:\program files' "
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

So, yours is …

PowerShell -Command "Get-Process java ^ | where {$_.path -like 'D:/ptc/Windchill_11.0/Java/jre/bin/java.exe'}"

PowerShell: Running Executables

When using PowerShell, understanding Quoting Rules is paramount.

PowerShell Quotes – To Expand Or Not Expand, That Is The Question

A Story of PowerShell Quoting Rules

Again, since you are new, spend the needed time on:

...and use all the no cost / free resources all over the web. Like the MS TechNet Windows PowerShell Survival Guide.

1
  • Thanks for all the info. I got the answer I needed, but I have started sudying powershell. Commented Mar 25, 2019 at 15:51

Not the answer you're looking for? Browse other questions tagged .