通过 CMD 检查 PC 和特定文件的网络可用性

问题描述

我有一些 PC 主机名的列表,我想简单检查一下本地网络中的 PC 是否在线,并检查特定文件是否在该 PC 中。 我通过 CMD 制作了一些“程序”。但它很懒,检查网络上的几台PC需要太长时间。

第一台PC(工作站)的命令示例:

::this first command will check if PC is online and it will save workstation's hostname to result.txt file for next used command.
wmic /FAILFAST:ON /user: "admin" /password:"123456" /node:"Workstation1.subdomain.domain" computersystem get "Name" | more >>result.txt

    ::this second command will check if specific file (for example: AcroRd32.exe) not exist and it will save result to result.txt if it is not exist. Problem is that the this part is executing too long if PC is offline.
if not exist "\\Workstation1.subdomain.domain\c$\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" echo File NOT EXIST! | more >>result.txt

result.txt 的输出应如下所示:

Worskation1.subdomain.domain
File NOT EXIST!

or

Worskation1.subdomain.domain
(empty line) 

1.是否可以使第二个命令更快?

  1. 是否可以通过 CMD 中的另一种方式解决这个问题?
  2. CMD 适合这份工作吗?
  3. 另一种解决方案?

解决方法

有一个简单的方法。

如果网络已启动且工作站已启动(并且您的凭据正确),则第一个命令 wmic /FAILFAST:ON /user: "admin" /password:"123456" /node:"Workstation1.subdomain.domain" computersystem get "Name" 将以状态代码 0 退出,如果出现问题,则执行其他任何操作。

所以你只需要检查退出代码:

::this first command will check if PC is online and it will save workstation's hostname to result.txt file for next used command.
set LOCALV_WORKSTATIONUP=0
( wmic /FAILFAST:ON /user: "admin" /password:"123456" /node:"Workstation1.subdomain.domain" computersystem get "Name"  | more >>result.txt ) && set LOCALV_WORKSTATIONUP=1

IF "%LOCALV_WORKSTATIONUP%" == "1" (
    ::this second command will check if specific file (for example: AcroRd32.exe) not exist and it will save result to result.txt if it is not exist. Problem is that the this part is executing too long if PC is offline.
if not exist "\\Workstation1.subdomain.domain\c$\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" echo File NOT EXIST! | more >>result.txt
)

如果您使用延迟扩展,请注意检查 !LOCALV_WORKSTATIONUP! 而不是 %LOCALV_WORKSTATIONUP% :

::this first command will check if PC is online and it will save workstation's hostname to result.txt file for next used command.
set LOCALV_WORKSTATIONUP=0
( wmic /FAILFAST:ON /user: "admin" /password:"123456" /node:"Workstation1.subdomain.domain" computersystem get "Name"  | more >>result.txt ) && set LOCALV_WORKSTATIONUP=1

IF "!LOCALV_WORKSTATIONUP!" == "1" (
    ::this second command will check if specific file (for example: AcroRd32.exe) not exist and it will save result to result.txt if it is not exist. Problem is that the this part is executing too long if PC is offline.
if not exist "\\Workstation1.subdomain.domain\c$\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" echo File NOT EXIST! | more >>result.txt
)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...