0

I'm trying to find a way to get a process' full command line in one string to detect if a java application is already running. The program allows for multiple instances, but I want to prevent opening more than one instance. The output of the command adds 4 return carriages which I would like to remove. Here's the what I used: $processRunning = Get-WmiObject Win32_Process -Filter "name= 'javaw.exe'" | select -ExpandProperty CommandLine

The problem I'm having is that the line above adds return carriages because the command line is very long. I've tried formating it differently with Format-Table and Format-List. I've also tried -replace "`r", "" and | Out-String -Width 2048.

I want to match the result like so: $startProcess = ([wmiclass]"win32_process").Create($processToStart) if ($processRunning -like $commandLine ) { Write-Output "Program is already running"
} else { Write-Output "Starting Process" $startProcess Wait-Process -Id $startProcess.ProcessId }

The Wait-Process command doesn't work because apparently $startProcess.ProcessId is empty or null.

Thank you

Narkael
  • 53
  • 1
  • 10
  • That result is returning as an array. You will need to join the array entries into a string if you want a single line. – Jason Snell Jul 12 '17 at 18:42
  • "The problem I'm having is that the line above adds return carriages" - what gives you the impression that the string contains embedded carriage returns? – Bill_Stewart Jul 12 '17 at 18:42
  • @Bill_Stewart Maybe it's not the right way to verify that but I copied-pasted the output from the console to a text file and it contained carriage returns. And the `if ($processRunning -like $starTeamCommandLine )` is never true even though I know that $processRunning contains $CommandLine – Narkael Jul 12 '17 at 18:52
  • @Jason I already tried -join "`r" and it didn't work. Maybe I just used it wrong...? – Narkael Jul 12 '17 at 18:54
  • You are correct; that's not the right way to verify. – Bill_Stewart Jul 12 '17 at 19:05

0 Answers0