问题描述
我有一些 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.是否可以使第二个命令更快?
或
解决方法
有一个简单的方法。
如果网络已启动且工作站已启动(并且您的凭据正确),则第一个命令 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
)