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

Checking network availability of PC and of specific file via CMD

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

第一台 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
)