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