Reputation: 131
I have a need to check if a file has not been modified in XX minutes, and if condition is true, then kill the PID that has the file locked. Only clean way to determine if a file has not been modified in XX minutes is in powershell but Only way i have found to kill the associated PID locking the file is using handle OS command. So, i am stuck with 1/2 batch and 1/2 powershell to accomplish what i want.
I have it working fine as a standalone command to kill the PID, but as soon as i put it all together inside braces, Powershell doesnt like it. Seems like it may not like putting the Stop-Parsing argument. Code is below.. Calculating the $inactive variable earlier in the script. That part is fine. Its the cmd batch code that doesnt like being in curly braces.
if ($inactive -eq 'True') {cmd --% /C for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula file.csv') do taskkill /t /f /PID %i}
if i just run the cmd line inside powershell alone but without the {} it works fine, but i need to evaluate on the $inactive variable first to determine if i need to run the command
Works fine..
cmd --% /C for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula file.csv') do taskkill /t /f /PID %i
Doesnt work..
if ($inactive -eq 'True') {cmd --% /C for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula file.csv') do taskkill /t /f /PID %i}
thank you
Upvotes: 2
Views: 248
Reputation: 437618
If you place the closing }
on the same line, it isn't recognized as the block's closing delimiter (it is treated as part of the unparsed command line following --%
, the stop-parsing symbol).
Place the }
on the next line to fix the problem:
if ($inactive -eq 'True') {
cmd --% /C for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula file.csv') do taskkill /t /f /PID %i
}
For more information on --%
, including its limitations and pitfalls, see this answer.
Upvotes: 3