OS Name Variable

送分小仙女□ 提交于 2019-12-02 14:11:56

问题


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 variable out of my windows name . I know what to run but where I am running into an issue is a place holder. so here is my code:

    :OS_NAME
    Set OS_NAME= systeminfo | find "OS Name"

    :OS_Ver
    Set OS_Version= systeminfo | findstr /B /C:"OS Version"

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

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

    :Win_7
    systeminfo | find "Microsoft Windows 7" > nul
        if %ERRORLEVEL% == 0 (
            goto ver_7)


    :Ver_7
    Set Win7= systeminfo | find "Microsoft Windows 7"
    Echo %computername% is running %WIN7% in %OS_ARCH% Environment >> G:\Directory\Win7Comps.txt

So basically I would like a place holder for the results of Systeminfo which i can refer to and parse it my SET command when I am making my system variables.

Thanks, any help would be appreaciated.


回答1:


Not exactly sure what you're looking for, but the biggest problem I see is that systeminfo takes forever to run and returns much more information than you're looking for. You'd be better off capturing wmi queries using wmic. This is basically a rewrite of your example script, just using wmic rather than systeminfo. It should be much, much faster.

@echo off
setlocal
set prefix=G:\Directory

for /f "usebackq tokens=1,2 delims==|" %%I in (`wmic os get name^,version /format:list`) do 2>NUL set "%%I=%%J"
for /f "tokens=2 delims==" %%I in ('wmic bios get version /format:list') do set "bios=%%I"
for /f "tokens=2 delims==" %%I in ('wmic computersystem get model /format:list') do set "model=%%I"

>>"%prefix%\%COMPUTERNAME%" echo OS Name: %name%
>>"%prefix%\%COMPUTERNAME%" echo OS Version: %version%
>>"%prefix%\%COMPUTERNAME%" echo PC Model: %model%
>>"%prefix%\%COMPUTERNAME%" echo BIOS Version: %bios%

if defined PROGRAMFILES(x86) (set arch=X64) else set arch=X86

if "%name%" neq "%name:Windows 8=%" (
    set out=%prefix%\Win8Comps.txt
) else if "%name%" neq "%name:Windows 7=%" (
    set out=%prefix%\Win7Comps.txt
) else if "%name%" neq "%name:Windows Vista=%" (
    set out=%prefix%\WinVistaComps.txt
) else if "%name%" neq "%name:Windows XP=%" (
    set out=%prefix%\WinXPComps.txt
)

>>"%out%" echo %COMPUTERNAME% is running %name% in %arch% environment

Type wmic /? for more info, and try wmic computersystem get /? or similar to see a list of items that can be queried under each class.


wmic is the Swiss Army knife of Windows. Fun fact: you can even use wmic to generate a web page table of installed software on a remote system.




回答2:


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



来源:https://stackoverflow.com/questions/16042339/os-name-variable

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