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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:34:57  来源:igfitidea点击:

Killing a process in Batch and reporting on success

windowsbatch-filecmderrorlevel

提问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 >nuldoes 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 nulnot 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 taskkillisreturning 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 taskkillis printing to stderrso 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>&1redirects the standard error output into the vast nothingness. if errorlevel 1checks for errorlevelbeing 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 errorlevelwith if %errorlevel%==is a quite bad idea, unless you're comparing to 0. The semantics for exit codes are that anythingnon-zero signals failure. Your assumption here just was that taskkillwould return 1on failure.

通常检查errorlevelwithif %errorlevel%==是一个非常糟糕的主意,除非您与 0 进行比较。退出代码的语义是anything非零表示失败。您在这里的假设只是失败后taskkill会返回1

Ans may I kindly ask why you are doing this in an endless loop? taskkillalready 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"
}