1

I am facing a strange situation inside a Windows batch script that I currently work on.

In this section I extract the PID of a parent process, using its child's name (%PROCESS_NAME%). It work well and I can echo PARENT_PID without problems :

for /f "usebackq tokens=2 delims==" %%a in (`wmic process where ^(name^='%PROCESS_NAME%'^) get parentprocessid /value`) do (
        set PARENT_PID=%%a
    )

echo !PARENT_PID!

Result show the expected value :

16392


The problem is in next line :

taskkill /f /t /pid !PARENT_PID!

The process doesn't get killed and the script showing a strange text (I am working in a french environment) :

" est introuvable.sus "16392


If I replace !PARENT_PID! with the hardcoded PID value like this :

taskkill /f /t /pid 16392

Everything works fine and the parent process and its child are killed.

I tried many thing but cannot figure out what is wrong. If anyone has an idea, I'm interested to kow, thanks !

  • 1
    Please show the all of the relevant code. You have not shown enough to diagnose the problem. We need to see the exact FOR /F loop as well as the ECHO and the TASKKILL, all in context. – dbenham Jan 13 '20 at 16:49
  • 1
    @dbenham - it's a thing that you've already found a fix for: https://www.dostips.com/forum/viewtopic.php?t=4266 – npocmaka Jan 13 '20 at 16:57
  • @npocmaka - Doh! Thanks – dbenham Jan 13 '20 at 17:16
  • ...and on [this site](https://stackoverflow.com/a/34695095) too. – Compo Jan 13 '20 at 19:07

2 Answers2

3

Try like this and you'll see the problem:

@echo off
setlocal enableDelayedExpansion
for /f "usebackq tokens=2 delims==" %%a in (`wmic process where "name='explorer.exe'" get parentprocessid /value`) do (
        set PARENT_PID=%%a
    )

echo -!PARENT_PID!-

WMIC output sets one additional carriage return at the end. Here you can find a workaround (with an additional for loop):

@echo off
setlocal enableDelayedExpansion
for /f "usebackq tokens=2 delims==" %%a in (`wmic process where "name='explorer.exe'" get parentprocessid /value`) do (
        for /f "tokens=* delims=" %%# in ("%%~a") do set "PARENT_PID=%%#"
    )

echo -!PARENT_PID!-
dbenham
  • 127,446
  • 28
  • 251
  • 390
npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

I've always opted to try to prevent an issue from happening, rather than fix it later. For that reason, here are some alternative suggestions for trying to overcome the unwelcome line feed issue.

If you can include a property before and after the target property, (in this case ParentProcessID), and be sure that those 'pre' and 'post' properties will not be NULL, you can retrieve your value without the problematic carriage return.

Example:

@Set "PPID="
@For /F "EOL=H Tokens=2" %%A In ('""%__AppDir__%wbem\WMIC.exe" Process Where Name="%PROCESS_NAME%" Get Handle,ParentProcessID,SessionID 2>NUL"')Do @Set "PPID=%%A"
@Set PPID 2>NUL
@Pause


Alternatively, you could use 's /Format option.

Example:

@Set "PPID="
@For /F "Skip=1 Tokens=2 Delims=," %%A In ('""%__AppDir__%wbem\WMIC.exe" Process Where Name="%PROCESS_NAME%" Get ParentProcessID /Format:CSV 2>NUL"') Do @Set "PPID=%%A"
@Set PPID 2>NUL
@Pause

Note: There's an issue that I'm aware of in , whereby the location of required .xsl files is not correct. One fix is to move those files into the correct directory, \Windows\System32\wbem. Alternatively find its location and specify it directly within the value. On the systems I've used, I found csv.xsl in \Windows\System32\wbem\en-US, so you could use, (adjusting as necessary):

@Set "PPID="
@For /F "Skip=1 Tokens=2 Delims=," %%A In ('""%__AppDir__%wbem\WMIC.exe" Process Where Name="%PROCESS_NAME%" Get ParentProcessID /Format:"%__AppDir__%wbem\en-US\csv.xsl" 2>NUL"') Do @Set "PPID=%%A"
@Set PPID 2>NUL
@Pause
Compo
  • 36,585
  • 5
  • 27
  • 39