批处理文件帮助 - 将命令输出添加到csv文件

时间:2013-10-18 10:27:57

标签: batch-file

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

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

`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`

由于 标记

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

中生成