OS Name Variable

前端 未结 2 1368
清歌不尽
清歌不尽 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 07:45

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题