【问题标题】:How to out put the PC name that has certain process running?如何输出运行某个进程的PC名称?
【发布时间】:2013-02-21 01:38:42
【问题描述】:

我正在使用 psexec 自动运行

get.bat


任务列表 | findstr pmill.exe >> dc-01\c$\0001.txt


run_get.bat


psexec @%1 -u 管理员 -p 密码 -c "C:\get.bat"


pclist.txt


在我们网络上的所有 PC 上,

如何在文本文件中获得 PC 名称而不是 pmill.exe 的结果? 无论如何我可以从powershell做吗? 我需要在结果中获得电脑名称。 请提示!

【问题讨论】:

    标签: powershell batch-file


    【解决方案1】:

    代替 psexec,试试这个:

    @echo off
    setlocal enabledelayedexpansion
    for /f %%I in (pclist.txt) do (
        set /p q="Checking %%I... "<NUL
        ping -n 1 -w 500 %%I>NUL 2>NUL
        if !errorlevel!==1 (
            echo Offline.
        ) else (
            wmic /node:%%I /user:adminuser /password:pass process where name="pmill.exe" get csname 2>NUL | find /i "%%I" >>dc-01\c$\0001.txt
            echo Done.
        )
    )
    

    如果 pmill.exe 正在运行,则输出 %computername%,否则不输出任何内容。

    编辑:

    如果您必须使用psexec,那么我建议您更改调用psexecfor 循环的逻辑,如下所示:

    @echo off
    setlocal enabledelayedexpansion
    for /f %%I in (pclist.txt) do (
        set pc=%%I
        set /p q="Checking %%I... "<NUL
        ping -n 1 -w 500 %%I>NUL 2>NUL
        if !errorlevel!==1 (
            echo Offline.
        ) else (
            for /f %%z in ('psexec \\!pc! -u adminuser -p pass tasklist 2^>^&1 ^| findstr /i "pmill.exe"') do (
                set /p q="pmill.exe found.  "<NUL
                echo !pc!>>dc-01\c$\0001.txt
            )
            echo Done.
        )
    )
    

    【讨论】:

    • 好的,我可以试试 wmic,但我仍然必须使用 psexec 在所有 PC 上自动运行。
    • 顺便说一句,2>nul 是什么意思?
    • 2&gt;NUL 表示将 stderr 重定向到黑洞。 wmic 无缘无故地通过 stderr 输出换行符。 2&gt;NUL 将它们压扁。在我更新的psexec 示例中,2&gt;&amp;1 将标准错误重定向到标准输出。请参阅Using command redirection operators 了解更多信息。
    • 再试一次。我忘了在 for 循环中转义 2^&gt;^&amp;1。如果您想尝试一下,我还完成了wmic 方法。根据我的经验,wmicpsexec 快得多。
    • 它现在正在醒来,但我得到的结果都是 %I
    猜你喜欢
    • 1970-01-01
    • 2017-12-26
    • 2016-11-09
    • 2011-09-02
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多