批处理脚本以通过“命令行”参数进行taskkill搜索

时间:2019-02-21 13:15:48

标签: powershell

我需要一个批处理脚本来通过“命令行” 自变量(Windows任务管理器中的“命令行”)进行任务杀死。需要说明的是-这些过程是 dotnet核心应用程序。它们通过以下方式启动:

dotnet MyDotnetCoreApp.dll xxx yyy

如果您在“任务管理器”下进行检查,

  1. 名称= dotnet.exe

  2. 图像路径名= C:\ Program Files \ dotnet \ dotnet.exe

  3. 命令行= dotnet MyDotnetCoreApp.dll xxx yyy

我需要一个批处理脚本来杀死这些任务,可能使用 taskkill

选项1 是Taskkill by PID,但是我的脚本如何搜索MyDotnetCoreApp的“命令行”参数?

选项2 是按图片名称显示的taskkill吗?这是不可行的,因为我的服务器有许多dotnet核心应用程序,如果杀死我的映像名称,则所有dotnet核心进程都会被杀死

我正在研究:

https://superuser.com/questions/415360/how-do-i-find-out-command-line-arguments-of-a-running-program

https://www.itprotoday.com/powershell/powershell-contains

我无法执行此操作,不擅长 PowerShell

// ..................VVVV
template<typename T, auto N> 
struct make_it<std::array<T, N>, std::enable_if_t<(N > 4)>>
{
    static std::array<T, N> apply()
    {
        return std::array<T,N>{};  
    }
};

这里会列出要杀死的PID。

两个挑战:

第一个挑战,我的WHERE子句无效:

Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | Select-Object Handle

我进一步检查,发现没有为这些WmiObjects填充这些“ CommandLine”(哎呀!):     Get-WmiObject Win32_Process -Filter“名称='dotnet.exe'” |选择对象ProcessId,名称,CSName,标题,命令行,ExecutablePath

后来我发现,如果您以管理员身份运行Powershell ,将填充“ CommandLine”! (Powershell太神秘了!)

最后-第一个挑战被解决

Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'} | Select-Object Handle

第二个挑战:如何杀死它? 找到了!!

Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetApp*'} | Select-Object ProcessId, Name, CSName, Caption, CommandLine, ExecutablePath 

所以这实际上已经解决了!

2 个答案:

答案 0 :(得分:0)

以管理员身份运行Powershell!从https://docs.microsoft.com/en-us/sysinternals/downloads/psexec

下载 psexec
<script type="text/javascript">
   let selectItem = document.getElementById('pneu');
   //let additionalRow = document.getElementById('additionalRow');
   let detached = '';
   function checkSelected() {
      if (selectItem.selectedIndex == "1") {
         detached = $('#reifenmontage-input').detach();
      } else {
         detached.appendTo('#additionalRow');
      }
   }
</script> 

<script type="text/javascript">
$('#button-getdata').on('change', function() {
    $.ajax({
        url: 'index.php?route=api/reifenmontage/get_marka_data',
        type: 'post',
        data: $('#reifenmontage-input select'),
        dataType: 'json',
        beforeSend: function() {

        },
        success: function(json) {

            if (json['success']) {
               $("#result").empty();
                for (i in json['success']) {
                var element = json['success'][i];
                var o = new Option(element['model'], element['model']);
               $("#result").append(o);
                    html = "\t<option  value=\""+ element['model'] + "\">" + element['model'] + "</option>\n";
                    $("#result").append(o); 

                }
               //  document.getElementById("schedule_form").reset();

            }   

        }
    });
});
</script>  

<script type="text/javascript">

$.ajax({
 url: 'index.php?route=api/reifenmontage/mark',
 context: document.body,
 success: function(data) {
   const selectControl = $('#button-getdata');
   selectControl.html(data.map(ExtractData).join(''));
 }
});

function ExtractData(item) {
return ` <option value="${item.value}">${item.label}</option>`;
}

</script>

然后从 Powershell 中获得

psexec -u Administrator -p SomeSecret powershell

现在作为单独的问题,您能做到这一点吗?由于-Filter中带有引号,因此以下内容将无法工作!

(Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate()

随着变通方法的解决,我删除了 -Filter 子句(不幸的是,不确定如何转义引号):

psexec -u Administrator -p SomeSecret powershell -Command "(Get-WmiObject Win32_Process -Filter ""name = 'dotnet.exe'"" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate() "

答案 1 :(得分:0)

以常规用户的身份为我工作,除非该过程以管理员身份运行?不幸的是,这里的过滤器语法类似于sql,其中'%'是通配符。管道到对象可能也一样工作。

get-wmiobject win32_process -filter "commandline like '%dotnet.exe%MyDotnetCoreApp%'" | 
  remove-wmiobject

get-wmiobject win32_process | where commandline -like '*dotnet.exe*MyDotnetCoreApp*' | 
  remove-wmiobject