getting server details in output csv file using batch script

家住魔仙堡 提交于 2020-01-07 02:24:43

问题


Below code gets the xml data output for each of the servers which is listed in serverFile file and gets the required tag values in csv format as output with required tag name as header. Am trying to achieve below requirement to make code more generic/output format readable ie,

a. make the ipaddress parameterised so, can use server_variable in wget line and in turn this can be run for all the servers which is present in serverFile

.\bin\wget64.exe -OC:\output.xml http://11.12.13.15/xmldata?get=AllRecord

in the above code line, I was thinking of making ipaddress parameterised something like read server info and save it in variable ie, server so, in the above line inplace of ip address we can use %server% something like this :

.\bin\wget64.exe -OC:\output.xml http://%server%/xmldata?get=AllRecord

b. 2nd point is, in current result csv file, getting all the required tags/values in csv format but somehow wanted to include ip address as well with header name as SERVER, I been suggested to use one more for loop and I did try but couldn't get the content of serverFile under SERVER header name

code :

 @Echo Off
    setlocal enabledelayedexpansion
    set serverFile="C:\serverlist.txt"
    set inFile="C:\output.xml"
    set outFile="C:\output.csv"
    if exist %outFile% del %outFile%
    rem set the header line
    set req_tags=SN,UUID,PN,TYPE
    set outLine=
    echo SN,UUID,PN,TYPE > %outFile%
      for /f "tokens=*" %%f in (%serverFile%) do (
      set serverIP=%%f
      echo --------- !serverIP! ------------------
      rem pause
.\bin\wget64.exe -OC:\output.xml http://11.12.13.15/xmldata?get=AllRecord
rem .\bin\wget64.exe -OC:\output.xml http://!serverIP!/xmldata?get=AllRecord
        for %%a in (%inFile%) do (
          for %%c in (%req_tags%) do (
            set search_tag=%%c
            for /f "tokens=2 delims=><  " %%b in ('type "%%a" ^|findstr /i !search_tag!' ) do (
              if [%%b] NEQ [] (
                rem we don't want to match /BSN
                if [%%b] NEQ [/BSN] (
                  set outline=!outline!%%b,
                )
              )
            )
          )
        )
      )
    rem output the values
    rem remove trailing ,
    set outline=%outline:~0,-1%
    echo %outline%>>"%outFile%"
    endlocal

output.csv content :

SERVER,SN,UUID,PN,TYPE 
Pro Gen8,Pro Gen8,ILD9V,IDC21E,Inte 4,Admin 

In the above output, under SERVER header, should get server ip address but currently am getting full path of the serverFile and not it;s content.

then under UUID header, xml file has 2 UUID tag under different parent so, in the output file am getting both UUID's next to eachother, which in turn moves output of next header ie, PN, TYPE to misallign. not sure if we can have some sub function where in which we can give reg_tag and parent tag(to look for) as argument so function will get only one result.


回答1:


You are using delayed expansion inconsistently:

  1. ECHO is off. comes from echo %ip% >> %outFile%:
    • should be echo !ip! >> %outFile%
  2. all *,= come from call :outputlinetocsv "%hLine%":
    • should be call :outputlinetocsv "!hLine!"
  3. for %%a in (%req_tags%) do (
    • either should be for %%a in (!req_tags!) do (
    • or you could keep for %%a in (%req_tags%) do ( but define set "req_tags=SP PN TYPE" out of the %%x loop i.e. before it.


来源:https://stackoverflow.com/questions/38722347/getting-server-details-in-output-csv-file-using-batch-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!