OS Name Variable

前端 未结 2 1396
清歌不尽
清歌不尽 2021-01-23 07:21

I would like to run a script where I can get the windows Name and version of the system of all computers running in the company, put it in a text fil. Then make a system variabl

2条回答
  •  独厮守ぢ
    2021-01-23 08:05

    You are giving batch a little too much credit. If you ran echo %OS_NAME% it would literally echo systeminfo | find "OS Name". Variables in batch will always just expand, it won't process any further. This will also more than likely try to run find "OS Name" when you try to set the variable as the | is not escaped nor enclosed in double quotes.

    If you want to set the output of a command to a value you have to capture it in a for statement like this:

    for /f "tokens=2 delims=:" %%a in ('systeminfo ^| find "OS Name"') do set OS_Name=%%a
    

    Then remove the leading spaces like this: (there is probably a better way to do this)

    for /f "tokens=* delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
    

    A few things to note here. 1.) "tokens=2 delims=:" is setting the delimiter to : and it is selecting the second section only, which will pull only the part you want. 2.) the | is escaped with a ^, this needs to be done in for loops or anything after that will attempt to execute as seperate commands. 3.) "tokens=* delims= " The token here is * which is all tokens.

    A few other problems I found.

    systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /c:"BIOS Version" |  >> G:\Directory\%Computername%
    

    This has an extra | character at the end.

    IF exist "%programfiles(x86)% (SET OS_ARCH=X64)
    Else (SET OS_ARCH=X86)
    

    Two problems here, you didn't finish the double quote around the path, and else has to be on the same line as the ending parentheses of the if statement, like this:

    IF exist "%programfiles(x86)% (SET OS_ARCH=X64
    ) Else (SET OS_ARCH=X86)
    

    Otherwise it tries to process else as it's own command

提交回复
热议问题