Windows 通过cmd杀死每个用户具有相同名称的2个进程
知道如何通过cmd杀死每个用户具有相同名称的2个进程的人Windows 通过cmd杀死每个用户具有相同名称的2个进程,windows,powershell,batch-file,cmd,Windows,Powershell,Batch File,Cmd,知道如何通过cmd杀死每个用户具有相同名称的2个进程的人 TASKLIST/V/FI“IMAGENAME eq cmd.exe” 就像: FOR /f IF #the user has the program open twice TASKKILL "PS" 假设两个进程的“窗口标题”均为=“无标题-记事本” 命令将是: taskkill/f/fi“WINDOWTITLE eq无标题-记事本”/fi“用户名eq您的用户名” 否则,您需要提供imagename或pid。 任务
TASKLIST/V/FI“IMAGENAME eq cmd.exe”
就像:
FOR /f
IF #the user has the program open twice
TASKKILL "PS"
假设两个进程的“窗口标题”均为=“无标题-记事本”
命令将是:
taskkill/f/fi“WINDOWTITLE eq无标题-记事本”/fi“用户名eq您的用户名”
否则,您需要提供imagename或pid。
任务列表/FI“IMAGENAME eq myapp.exe/FI“username eq yourusername”请参阅@MC ND的回答
只需测试tasklist
输出中的任何一行是否通过了两个过滤器,即account和executable
如果未找到进程,请继续
如果找到进程,则对其进行一次指定并递增循环计数器
如果达到限制,请终止该进程,否则请等待并重复该进程
@ECHO关闭
设置“MyProcess=wscript.exe”
设置“循环=0”
设置“loopLimit=2”
:PRVARCHSTART
设置“进程=”
@对于/f“令牌=2个delims=,”%%a in(
'Tasklist/v/fo:csv/nh^ findstr/l/c:“%username%”^ findstr/l/c:“%MyProcess%”
)做(
设置/a“循环+=1”
设置“进程=%%~a”
)
如果未定义流程转到:PRVARCHEND
如果%loop%geq%loopLimit%(
Taskkill/pid%进程%/f
后藤:普瓦钦德
)
Ping-n1localhost>nul2>nul
转到:PRVARCSTART
:PRVARCHEND
暂停和退出
我有一个旧的vbscript,它可以帮助您选择要通过其命令行终止的进程
Option Explicit
Dim Titre,Copyright,fso,ws,NomFichierLog,temp,PathNomFichierLog,OutPut,Count
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,_
"There is an existing proceeding !"
WScript.Quit
Else
Copyright = "[Hackoo "& chr(169) & " 2015]"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject( "Wscript.Shell" )
NomFichierLog="Killed_Process.txt"
temp = ws.ExpandEnvironmentStrings("%temp%")
PathNomFichierLog = temp & "\" & NomFichierLog
Set OutPut = fso.CreateTextFile(temp & "\" & NomFichierLog,1)
Call Find("wscript.exe")
Call Find("cmd.exe")
Call Explorer(PathNomFichierLog)
End If
'***************************************************************************************************
Function Explorer(File)
Dim ws
Set ws = CreateObject("wscript.shell")
ws.run "Explorer "& File & "\",1,True
end Function
'***************************************************************************************************
Sub Find(MyProcess)
Dim colItems,objItem,Processus,Question,Msg
Titre = " Process(es) "& DblQuote(MyProcess) &" running "
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like " & CommandLineLike(WScript.ScriptFullName) & "",,48)
Count = 0
For Each objItem in colItems
Count= Count + 1
Processus = objItem.CommandLine
Question = MsgBox ("Would do you like to kill this script : " & DblQuote(Processus) & " ?" ,VBYesNO+VbQuestion,Titre+Copyright)
If Question = VbYes then
objItem.Terminate(0)'To kill the process
OutPut.WriteLine Processus
else
Count= Count - 1 'decrementing the count of -1
End if
Next
OutPut.WriteLine String(100,"*")
If Count > 1 Then
Msg = " were killed"
Else
Msg = " was killed"
End if
OutPut.WriteLine count & Titre & Msg
OutPut.WriteLine String(100,"*") & VbCrLF
End Sub
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'**************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'**************************************************************************
Powershell方式:
Get Process-Name notepad | Stop Process
。实际上它只是Stop Process-Name notepad
。您能更具体一点吗?这是标记为cmd
和Powershell
,您是否只在寻找cmd
解决方案?您是否试图为用户杀死重复的进程?如果它解决了问题,我会t无论是powershell还是CMD,是否打开命令提示符窗口,键入taskkill/?
,然后按[ENTER]
?在执行此操作时,您应该注意,imagename
和username
都有过滤器,并有示例显示如何使用它们。此外,您还可以使用tasklist/?
和/或qprocess/?
执行相同的操作。您可能希望将所选示例与管道组合,并执行协同操作使用find/?
或findstr/?
解除锁定。如果使用WMIC和带命令行的筛选器,可能会发生这种情况?