windows 批量终止进程并报告成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1896262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Killing a process in Batch and reporting on success
提问by Deniz Zoeteman
i have the following batch file, which terminates the iTunes program so, that if i connect my iPod, it's not going to sync it. (I know you can set this up in iTunes.)
我有以下批处理文件,它会终止 iTunes 程序,因此,如果我连接 iPod,它不会同步它。(我知道您可以在 iTunes 中进行设置。)
@echo off
:kill
cls
taskkill /F /IM itunes.exe >nul
if %errorlevel%==1 {
echo iTunes not found.
} else {
echo iTunes is killed.
}
goto kill
However, the >nul
does not respond to the command; so it just gives the default command text. So yeah, what i want to do:
但是,>nul
不响应命令;所以它只提供默认的命令文本。所以是的,我想做的是:
If iTunes is not found, as given by the command, it should display
如果未找到 iTunes,则如命令所示,应显示
iTunes not found
找不到 iTunes
If it is found and terminated,
如果被发现并终止,
iTunes is killed
iTunes被杀
Help? the errorlevel's don't work, this seem to be the fault of the nul
not working.
帮助?错误级别不起作用,这似乎是nul
不工作的错。
回答by Joey
Works for me at least:
至少对我有用:
> taskkill /f /im powershell.exe && echo worked || echo not worked SUCCESS: The process "powershell.exe" with PID 3228 has been terminated. worked > taskkill /f /im powershell.exe && echo worked || echo not worked ERROR: The process "powershell.exe" not found. not worked
So taskkill
isreturning a proper exit code. The redirect of its output has nothing to do with this. But the error level for failure is 128. You really should use the proper idiom for checking for errors.
因此,taskkill
在返回了相应的退出代码。其输出的重定向与此无关。但是失败的错误级别是 128。您确实应该使用正确的习惯用法来检查错误。
Also it seems that taskkill
is printing to stderr
so you see its output still, when just redirecting stdout
. You may consider rewriting above code to:
此外,它似乎taskkill
正在打印,stderr
因此您在重定向stdout
. 您可以考虑将上面的代码重写为:
taskkill /F /IM itunes.exe >nul 2>&1
if errorlevel 1 (echo iTunes not found.) else (echo iTunes is killed.)
The 2>&1
redirects the standard error output into the vast nothingness. if errorlevel 1
checks for errorlevel
being at least 1which should work at this point:
该2>&1
重定向标准错误输出到广阔的虚无。if errorlevel 1
支票errorlevel
是至少1,应在这一点上工作:
ERRORLEVEL numberSpecifies a true condition if the last program run returned an exit code equal to or greater than the number specified. —
help if
ERRORLEVEL number如果上次运行的程序返回的退出代码等于或大于指定的数字,则指定真条件。—
help if
Generally checking errorlevel
with if %errorlevel%==
is a quite bad idea, unless you're comparing to 0. The semantics for exit codes are that anything
non-zero signals failure. Your assumption here just was that taskkill
would return 1
on failure.
通常检查errorlevel
withif %errorlevel%==
是一个非常糟糕的主意,除非您与 0 进行比较。退出代码的语义是anything
非零表示失败。您在这里的假设只是失败后taskkill
会返回1
。
Ans may I kindly ask why you are doing this in an endless loop? taskkill
already kills allinstances of itunes.exe
. And you're running in a tight loop without any delays so your batch files probably consumes one CPU core while it's running.
Ans 请问您为什么要无限循环地执行此操作?taskkill
已经杀死了所有的实例itunes.exe
。而且您在一个紧凑的循环中运行,没有任何延迟,因此您的批处理文件在运行时可能会消耗一个 CPU 内核。
ETA:Overlooked your edit: Why on earth curly braces? Blocks in batch files are delimieted by round parentheses.
ETA:忽略了您的编辑:到底为什么要花括号?批处理文件中的块由圆括号分隔。
回答by ghostdog74
an alternative solution, in vbscript, save below code as mykill.vbs
另一种解决方案,在 vbscript 中,将下面的代码保存为 mykill.vbs
Set objArgs = WScript.Arguments
strProcess = objArgs(0)
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name ='" & strProcess & "'")
If colProcesses.Count = 0 Then
Wscript.Echo strProcess & " is not running."
Else
Wscript.Echo strProcess & " is running."
'Kill the process
For Each objProcess in colProcesses
r=objProcess.Terminate()
WScript.Echo "r is " & r
If r = 0 Then
WScript.Echo strProcess & " killed"
End If
Next
End If
on the command line
在命令行上
c:\test> cscript //nologo mykill.vbs "itunes.exe"
回答by Rosberg Linhares
Why don't you use PowerShell?
为什么不使用 PowerShell?
try
{
Stop-Process -Name itunes -ErrorAction Stop
"iTunes is killed"
}
catch
{
"iTunes not found"
}