1

我目前有一个批处理文件,它读取计算机名称列表并 ping 每一个并输出那些回复带有计算机名称和 IP 地址的 csv 文件的文件。

我现在需要编辑它以找出机器的用户。我需要联系在线用户以安排在他们的计算机上完成的一些工作。他们的批处理文件可以是一百多台机器,因此手动找出每个用户需要时间。有没有办法做到这一点?

`IF EXIST C:\test\new.csv (del C:\test\new.csv)
 IF EXIST C:\test\final.csv (del C:\test\final.csv)
 set ComputerList=C:\test\ClientList.txt
 Echo Computer Name,IP Address>Final.csv
 setlocal enabledelayedexpansion
 echo please wait...
 for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
 for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
 set IPadd=%%B
 echo %%A,!IPadd:~0,-1!>>final.csv
 )
 )
 findstr /V "IPAddress" final.csv >> C:\test\new.csv
 echo identified machines for Install
 start excel C:\test\new.csv
 echo opened csv file`

我想用来获取用户名的命令是:

 `wmic.exe /NODE: %%A COMPUTERSYSTEM GET USERNAME`

谢谢马克

4

2 回答 2

1

这是我写的一个函数来做你想做的事情:

    :GetLoggedInUser comp user
    for /f %%u in (
    'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
    if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
        'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do  (
          For /f %%b in ("%%a") do (set %2=%%b))    
    ) ELSE (for /f "skip=1" %%a in (
        'wmic /NODE:"%1" Computersystem get username' ) do ( 
          For /f %%b in ("%%a") do (set %2=%%b))
    ))  
    Exit /b

这是我的ping功能。如果 ping 成功则返回 0,否则返回 1。

    :IsPingable comp
    ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul  
    exit /b

    Usage example:

    for /l %%a in (1,1,255) do (
      call:IsPingable 10.6.1.%%a && (
      echo ping 10.6.1.%%a used )||( echo ping 10.6.1.%%a unused )
    )

如果您正在 ping IP 并且还想返回主机名,这适用于:

    :IsPingable2 comp ret
    setlocal
    for /f "tokens=2"  %%a in (
      '"ping -a -n 1 -4 "%~1" | Find "Pinging" 2>nul"') do set name=%%a
    endlocal & set %~2=%name%
    ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul  
    exit /b

    Usage example:

    @echo off
    setlocal enabledelayedexpansion
    for /l %%a in (1,1,255) do (
      call:IsPingable2 10.6.1.%%a host && (
      echo ping !host! - 10.6.1.%%a used )||( echo ping !host! - 10.6.1.%%a unused )
    )

我只是发布了这些,因为它们将来可能会派上用场。您现在可以使用 :IsPingable 了。

您可以在代码中这样使用它:

 IF EXIST C:\test\final.csv (del C:\test\final.csv)
 set ComputerList=C:\test\ClientList.txt
 Echo Computer Name,IP Address,Logged In User>Final.csv
 setlocal enabledelayedexpansion
 echo please wait...
 echo.
 for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
   for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|find "TTL="') do (
      if not errorlevel 1 (
         set IPadd=%%B
         call :GetLoggedInUser %%B uname
         echo %%A,!IPadd:~0,-1!,!uname!>>final.csv
      )
   )
 )
 echo identified machines for Install
 start excel C:\test\final.csv
 echo opened csv file
 goto :eof

:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
    'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do  (
      For /f %%b in ("%%a") do (set %2=%%b))    
) ELSE (for /f "skip=1" %%a in (
    'wmic /NODE:"%1" Computersystem get username' ) do ( 
      For /f %%b in ("%%a") do (set %2=%%b))
))  
Exit /b
于 2013-10-18T10:45:09.637 回答
0

下面的代码将按顺序计算两个文件中的行数,并设置为变量SalaryCountTaxCount.

@ECHO OFF    
echo Process started, please wait...    
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set SalaryCount=%%C    
echo Salary,%SalaryCount%
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set TaxCount=%%C
echo Tax,%TaxCount%

现在,如果您需要将这些值输出到 csv 文件,您可以使用以下代码。

@ECHO OFF
cd "D:\CSVOutputPath\"
echo Process started, please wait...
echo FILENAME,FILECOUNT> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
echo Salary,%Count%>> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
echo Tax,%Count%>> SUMMARY.csv

>覆盖文件的现有内容,>>并将新数据附加到现有数据中。CSV 将在D:\CSVOutputPath中生成

于 2016-09-03T11:07:09.217 回答