批处理脚本相关的问题 [英] Batch Script related issue

查看:173
本文介绍了批处理脚本相关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了下面的脚本其中工程精绝我的系统上。

I have written following script which works absolutely fine on my system.

@echo off

setlocal enabledelayedexpansion

FOR /F %%i IN ('wmic /node:%1 computersystem get Name') DO SET A=%%i
FOR /F %%i IN ('wmic /node:%1 computersystem get Domain') DO SET B=%%i
FOR /F %%i IN ('wmic /node:%1 computersystem get UserName') DO SET C=%%i
FOR /F %%i IN ('wmic /node:%1 computersystem get Manufacturer') DO SET D=%%i
FOR /F "delims=" %%i IN ('wmic /node:%1 computersystem get Model') DO SET E=%%i
FOR /F %%i IN ('wmic /node:%1 computersystem get SystemType') DO SET F=%%i
FOR /F %%i IN ('wmic /node:%1 bios get SerialNumber') DO SET G=%%i
FOR /F "delims=|" %%i IN ('wmic /node:%1 os get Name') DO SET H=%%i
FOR /F %%i IN ('wmic /node:%1 os get TotalVisibleMemorySize') DO (SET J=%%i)

SET /a J=%J%/1024

FOR /F "delims=" %%i IN ('wmic /node:%1 cpu get Name') DO SET K=%%i
echo %A%,%B%,%C%,%D%,%E%,%F%,%G%,%H%,%J% MB,%K% >> output.csv

但是,在它的一些变型将在下面示出,它并没有显示任何信息。

But, with some modifications in it which are shown below, it doesn't display any information.

@echo off

setlocal enabledelayedexpansion

ping -n 1 %1 | find "TTL=" > NUL
IF NOT ERRORLEVEL 1 (

FOR /F %%i IN ('wmic /node:%1 computersystem get Name') DO (SET A=%%i)
FOR /F %%i IN ('wmic /node:%1 computersystem get Domain') DO (SET B=%%i)
FOR /F %%i IN ('wmic /node:%1 computersystem get UserName') DO (SET C=%%i)
FOR /F %%i IN ('wmic /node:%1 computersystem get Manufacturer') DO (SET D=%%i)
FOR /F "delims=" %%i IN ('wmic /node:%1 computersystem get Model') DO (SET E=%%i)
FOR /F %%i IN ('wmic /node:%1 computersystem get SystemType') DO (SET F=%%i)
FOR /F %%i IN ('wmic /node:%1 bios get SerialNumber') DO (SET G=%%i)
FOR /F "delims=|" %%i IN ('wmic /node:%1 os get Name') DO (SET H=%%i)
FOR /F %%i IN ('wmic /node:%1 os get TotalVisibleMemorySize') DO (SET J=%%i)

SET J=%J%/1024

FOR /F "delims=" %%i IN ('wmic /node:%1 cpu get Name') DO (SET K=%%i)

echo %A%,%B%,%C%,%D%,%E%,%F%,%G%,%H%,%J%,%K% >> output.csv

)

这是不给任何错误无论是。但是,生成的 output.csv 文件不包含任何数据。

推荐答案

延迟扩展

在cmd中解析器到达行线(括号surounded行)的块,所有变量读取被替换他们在分析时的价值,之前执行块行。如果块内的变量的值发生变化,这种变化是没有看到在其中的值是readed线,因为这读取用变量的变化前的值所替代。

When the cmd parser reaches a line of a block of lines (lines surounded in parenthesis), all variable reads are replaced with the value they have at parse time, before executing the lines in the block. If the value of the variable changes inside the block, this change is not seen in the lines where the value is readed, as this read was replaced with the value of the variable before the change.

因此​​,在第二个code时,变量改变其值,但在最后的回波到输出文件,作为可变读取其中,与所述值变化前的值所替代,没有数据被输出。变量没有块的执行开始前的任意值。

So, in your second code, the variables are changing its value, but in the final echo to the output file, as the variable reads where replaced with the value before the value change, no data is output. The variables did not have any value before the start of block execution.

如果你想保持code的这种风格,你需要启用延迟扩展,并更改sintax在变量读取需要它的读取/值扩展的推迟到执行时间。

If you want to maintain this style of code, you need to enable delayed expansion, and change the sintax in the variable reads that need its reads / value expansion delayed until execution time.

setlocal enabledelayedexpansion
....
if not errorlevel 1 (
    ....
    echo !a!, !b!, !c!
)

算术

SET J =引用%j%/ 1024 SET / AJ =!J 1/1024 ,算术要求 SET / A

PING

正如JamesL表示在他的回答,在IPv6中的情况下,不存在的TTL =数据中的ping命令的输出。

As JamesL indicates in his answer, in the case of IPv6, there is no "TTL=" data in the output of the ping command.

在IPv4 ping命令集ERRORLEVEL如有包装丢失。你不会得到执行ping非活跃的同一子网设备时丢失的数据包。因此,在IPv4的测试活动机器的最佳方式是测试 TTL = 的产值。

In ipv4 ping command set errorlevel if any packed is lost. And you will not get packets lost when pinging a non active same subnet machine. So the best way of testing for active machine in ipv4 is to test for TTL= value in output.

在IPv6中,没有 TTL 输出,但现在,ping一台同一子网的非活跃机器,你得失去了所有的数据包。如果所有的数据包丢失的错误级别时,才设置。如果有任何数据包到达目标,错误级别未设置

In ipv6, there is not TTL output, but now, pinging a same subnet non active machine you obtain all packet lost. And the errorlevel is only set if ALL the packets are lost. If any packet reaches its target, errorlevel is not set

所以,对于IPv4,测试 TTL = ,用于IPv6测试错误级别

So, for ipv4, test TTL=, for ipv6 test errorlevel

WMIC

在检索数据的方式是正确的,会工作,更有效检索在单个查询所有可能的数据(如你在previous问题一样)。六呼吁 COMPUTERSYSTEM 数据应该被写成一个WMIC电话。

While this way of retrieving data is correct and will work, is more efficient to retrieve all possible data in a single query (as you did in your previous questions). The six calls for computersystem data should be written as a single wmic call.

这篇关于批处理脚本相关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆