Run VBS Script / Code from WITHIN Batch Script.

Page 1 of 2 12 LastLast

  1. Posts : 18,034
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #1

    Run VBS Script / Code from WITHIN Batch Script.


    I came across a folder that I haven't used for a couple of years, although I use the VBS Script, that containes all the documentation to find out what a particular Device [ Computer ] is [ Desktop, Laptop, Tablet, etc ] using the ChassisTypes Parameter. Please don't laugh as I do NOT use VBS, although I managed to put this together which works!

    In my notes, I mentioned that there were MANY sites where you could perform this process using the Battery Parameter Get-wmiObject win32_Battery, but obviously the results are limited to whether the Device has a Battery or NOT, therefore, excluding ALL the other criteria that is available.

    Now, Is there an easy way to run the below code from WITHIN a Batch Script and then output the result using echo for example please. This just produces the result in a Windows Script Host box?

    Code:
    
    ' ################################
    ' # Title     : Find Device Type #
    ' # Created By: Paul Black       #
    ' # Created On: 16-Oct-2019      #
    ' ################################
    dim wmi :set wmi=GetObject("WinMgmts:{ImpersonationLevel=Impersonate}!\\.\root\cimv2")
    dim Comp_Type, Col, Obj
    
    for each Col in wmi.ExecQuery("select * from Win32_SystemEnclosure")
      for each Obj in Col.ChassisTypes
        select Case Obj
          Case 1  :Comp_Type = "Other"
          Case 2  :Comp_Type = "Unknown"
          Case 3  :Comp_Type = "Desktop"
          Case 4  :Comp_Type = "Low Profile Desktop"
          Case 5  :Comp_Type = "Pizza Box"
          Case 6  :Comp_Type = "Mini Tower"
          Case 7  :Comp_Type = "Tower"
          Case 8  :Comp_Type = "Portable"
          Case 9  :Comp_Type = "Laptop"
          Case 10 :Comp_Type = "Notebook"
          Case 11 :Comp_Type = "Handheld"
          Case 12 :Comp_Type = "Docking Station"
          Case 13 :Comp_Type = "All-in-One"
          Case 14 :Comp_Type = "Sub-Notebook"
          Case 15 :Comp_Type = "Space Saving"
          Case 16 :Comp_Type = "Lunch Box"
          Case 17 :Comp_Type = "Main System Chassis"
          Case 18 :Comp_Type = "Expansion Chassis"
          Case 19 :Comp_Type = "Sub-Chassis"
          Case 20 :Comp_Type = "Bus Expansion Chassis"
          Case 21 :Comp_Type = "Peripheral Chassis"
          Case 22 :Comp_Type = "Storage Chassis"
          Case 23 :Comp_Type = "Rack Mount Chassis"
          Case 24 :Comp_Type = "Sealed-Case PC"
          Case else 
          Comp_Type = "Unknown"
        end select
      next
    next
    
    WScript.echo "This Device is a: " & Comp_Type
    
    
    

    Thanks.
    Last edited by Paul Black; 09 Mar 2022 at 19:02.
      My Computer


  2. Posts : 1,848
    Windows 10 Pro (+ Windows 10 Home VMs for testing)
       #2

    Perhaps run the VBS script from a BAT file, redirecting its output to a file then use type <filename> to echo the result to screen? I know it's messy ('cos you'll have to clear up the file) but, without using PowerShell, I can't think of another way.

    The problem is you can echo the output to the clipboard (using clip.exe) but CMD doesn't have a way of retrieving it back from the clipboard.

    The obvious answer is to use PowerShell, for example:

    Run VBS Script / Code from WITHIN Batch Script.-get_chassis_type_with_powershell.png

    You can output to screen, pipe to clipboard, retrieve from clipboard, format the output...
      My Computer


  3. Posts : 18,034
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #3

    Thanks for the reply @RickC, it is appreciated.

    RickC said:
    The obvious answer is to use PowerShell, for example:
    Yes, I have got a PowerShell version . . .

    Code:
    
    Function Get-ChassisType() {
      $ChassisTypes = @{
        Name = 'ChassisType'
        Expression = {
          # Property is an Array, so process ALL Values . . .
          $Result = ForEach($Value in $_.ChassisTypes)
          {
            Switch([int]$Value)
            {
              1       {'Other'}
              2       {'Unknown'}
              3       {'Desktop'}
              4       {'Low Profile Desktop'}
              5       {'Pizza Box'}
              6       {'Mini Tower'}
              7       {'Tower'}
              8       {'Portable'}
              9       {'Laptop'}
              10      {'Notebook'}
              11      {'Hand Held'}
              12      {'Docking Station'}
              13      {'All in One'}
              14      {'Sub Notebook'}
              15      {'Space-Saving'}
              16      {'Lunch Box'}
              17      {'Main System Chassis'}
              18      {'Expansion Chassis'}
              19      {'SubChassis'}
              20      {'Bus Expansion Chassis'}
              21      {'Peripheral Chassis'}
              22      {'Storage Chassis'}
              23      {'Rack Mount Chassis'}
              24      {'Sealed-Case PC'}
              Default {"$Value"}
            }
          }
          $Result
        }
      }
      Return (Get-CimInstance -ClassName Win32_SystemEnclosure | Select-Object -Property $ChassisTypes).ChassisType
    }
    Write-Host -NoNewline 'This Device is a: '; (Get-ChassisType)
    pause
    
    
    

    But, is there a easy way to run it from WITHIN the Batch Script Code itself?
    Last edited by Paul Black; 09 Mar 2022 at 19:14.
      My Computer


  4. Posts : 1,848
    Windows 10 Pro (+ Windows 10 Home VMs for testing)
       #4

    Paul Black said:
    But, is there a easy way to run it from WITHIN the Batch Script?
    It's easy to call the PowerShell script from within a batch script. The difficulty is getting the PS output as I don't know of a way to share data easily between the two.

    You can't pipe the PS output to the clipboard as there's no native way to read the clipboard contents from CMD (that I know of).

    That leaves you with piping the PS output to a file then using type <filename> within the BAT to read the file contents.
      My Computer


  5. Posts : 18,034
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #5

    Hello @RickC,

    RickC said:
    It's easy to call the PowerShell script from within a batch script. The difficulty is getting the PS output as I don't know of a way to share data easily between the two.

    You can't pipe the PS output to the clipboard as there's no native way to read the clipboard contents from CMD (that I know of).

    That leaves you with piping the PS output to a file then using type <filename> within the BAT to read the file contents.
    Thanks, I understand that, and as an easy option I can certainly do that.

    I don't think I have explained what I am trying to do very clearly. What I would ideally like is to incorporate the code directly into the Batch Script, and NOT have to call a separate .vbs or .ps1 file.

    I might have a go at writing a Batch Script later in the week using set /p.
    Last edited by Paul Black; 10 Mar 2022 at 04:49.
      My Computer


  6. Posts : 18,034
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #6

    Just for information, it appears that the ChassisTypes has been updated since I did any work on this . . .

    Code:
    
    ChassisTypes =
    
        1='Other'
        2='Unknown'
        3='Desktop'
        4='Low Profile Desktop'
        5='Pizza Box'
        6='Mini Tower'
        7='Tower'
        8='Portable'
        9='Laptop'
        10='Notebook'
        11='Hand Held'
        12='Docking Station'
        13='All-in-One'
        14='Sub Notebook'
        15='Space-Saving'
        16='Lunch Box'
        17='Main System Chassis'
        18='Expansion Chassis'
        19='Sub Chassis'
        20='Bus Expansion Chassis'
        21='Peripheral Chassis'
        22='Storage Chassis'
        23='Rack Mount Chassis'
        24='Sealed-Case PC'
        25='Multi-System Chassis'
        26='Compact PCI'
        27='Advanced TCA'
        28='Blade'
        29='Blade Enclosure'
        30='Tablet'
        31='Convertible'
        32='Detachable'
        33='IoT Gateway'
        34='Embedded PC'
        35='Mini PC'
        36='Stick PC'
    
    
    
    Last edited by Paul Black; 09 Mar 2022 at 19:04.
      My Computer


  7. Posts : 18,034
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #7

    OK, I wrote this Batch Script which works for me. It includes ALL the updated ChassisTypes criteria as posted above . . .

    Code:
    
    @echo off
    :: ################################
    :: # Title     : Find Device Type #
    :: # Created By: Paul Black       #
    :: # Created On: 09-Mar-2022      #
    :: ################################
    setlocal EnableDelayedExpansion
    
    set Dev_Type[1]=Other
    set Dev_Type[2]=Unknown
    set Dev_Type[3]=Desktop
    set Dev_Type[4]=Low Profile Desktop
    set Dev_Type[5]=Pizza Box
    set Dev_Type[6]=Mini Tower
    set Dev_Type[7]=Tower
    set Dev_Type[8]=Portable
    set Dev_Type[9]=Laptop
    set Dev_Type[10]=Notebook
    set Dev_Type[11]=Hand Held
    set Dev_Type[12]=Docking Station
    set Dev_Type[13]=All-in-One
    set Dev_Type[14]=Sub Notebook
    set Dev_Type[15]=Space-Saving
    set Dev_Type[16]=Lunch Box
    set Dev_Type[17]=Main System Chassis
    set Dev_Type[18]=Expansion Chassis
    set Dev_Type[19]=Sub Chassis
    set Dev_Type[20]=Bus Expansion Chassis
    set Dev_Type[21]=Peripheral Chassis
    set Dev_Type[22]=Storage Chassis
    set Dev_Type[23]=Rack Mount Chassis
    set Dev_Type[24]=Sealed-Case PC
    set Dev_Type[25]=Multi-System Chassis
    set Dev_Type[26]=Compact PCI
    set Dev_Type[27]=Advanced TCA
    set Dev_Type[28]=Blade
    set Dev_Type[29]=Blade Enclosure
    set Dev_Type[30]=Tablet
    set Dev_Type[31]=Convertible
    set Dev_Type[32]=Detachable
    set Dev_Type[33]=IoT Gateway
    set Dev_Type[34]=Embedded PC
    set Dev_Type[35]=Mini PC
    set Dev_Type[36]=Stick PC
    
    for /f "usebackq delims=" %%a in (`PowerShell "Get-wmiObject Win32_SystemEnclosure | Select-Object -ExpandProperty ChassisTypes"`) do (
      for /f "delims=" %%b in ("%%a") do (
        if not "%%b"=="" echo  This Device is a: !Dev_Type[%%a]!
      )
    )
    pause & Exit
    
     This Device is a: Notebook
    
    
    

    You can of course, include -ComputerName LocalHost in the PowerShell Command.
    Last edited by Paul Black; 09 Mar 2022 at 19:09.
      My Computer


  8. Posts : 456
    Windows 10
       #8

    Unless I got something wrong here you don't need powershell or VBS at all.

    Code:
    @echo off
    
    for /f "skip=1 Delims={}" %%a in  ('wmic path win32_SystemEnclosure get ChassisTypes') do (
                                                                                               set ChassisType=%%a
                                                                                               goto :Message
                                                                                              ) 
    
    :Message
    If %ChassisType%  EQU 1 set Dev_Type=Other
    If %ChassisType%  EQU 2 set Dev_Type=Unknown
    If %ChassisType%  EQU 3 set Dev_Type=Desktop
    If %ChassisType%  EQU 4 set Dev_Type=Low Profile Desktop
    If %ChassisType%  EQU 5 set Dev_Type=Pizza Box
    If %ChassisType%  EQU 6 set Dev_Type=Mini Tower
    If %ChassisType%  EQU 7 set Dev_Type=Tower
    If %ChassisType%  EQU 8 set Dev_Type=Portable
    If %ChassisType%  EQU 9 set Dev_Type=Laptop
    If %ChassisType%  EQU 10 set Dev_Type=Notebook
    If %ChassisType%  EQU 11 set Dev_Type=Hand Held
    If %ChassisType%  EQU 12 set Dev_Type=Docking Station
    If %ChassisType%  EQU 13 set Dev_Type=All-in-One
    If %ChassisType%  EQU 14 set Dev_Type=Sub Notebook
    If %ChassisType%  EQU 15 set Dev_Type=Space-Saving
    If %ChassisType%  EQU 16 set Dev_Type=Lunch Box
    If %ChassisType%  EQU 17 set Dev_Type=Main System Chassis
    If %ChassisType%  EQU 18 set Dev_Type=Expansion Chassis
    If %ChassisType%  EQU 19 set Dev_Type=Sub Chassis
    If %ChassisType%  EQU 20 set Dev_Type=Bus Expansion Chassis
    If %ChassisType%  EQU 21 set Dev_Type=Peripheral Chassis
    If %ChassisType%  EQU 22 set Dev_Type=Storage Chassis
    If %ChassisType%  EQU 23 set Dev_Type=Rack Mount Chassis
    If %ChassisType%  EQU 24 set Dev_Type=Sealed-Case PC
    If %ChassisType%  EQU 25 set Dev_Type=Multi-System Chassis
    If %ChassisType%  EQU 26 set Dev_Type=Compact PCI
    If %ChassisType%  EQU 27 set Dev_Type=Advanced TCA
    If %ChassisType%  EQU 28 set Dev_Type=Blade
    If %ChassisType%  EQU 29 set Dev_Type=Blade Enclosure
    If %ChassisType%  EQU 30 set Dev_Type=Tablet
    If %ChassisType%  EQU 31 set Dev_Type=Convertible
    If %ChassisType%  EQU 32 set Dev_Type=Detachable
    If %ChassisType%  EQU 33 set Dev_Type=IoT Gateway
    If %ChassisType%  EQU 34 set Dev_Type=Embedded PC
    If %ChassisType%  EQU 35 set Dev_Type=Mini PC
    If %ChassisType%  EQU 36 set Dev_Type=Stick PC
    
    echo.
    echo  I'm a "%Dev_Type%"
    echo.
    pause
      My Computer


  9. Posts : 7,610
    Windows 10 Home 20H2
       #9

    He needs neither PowerShell nor VBScript.
    He just enjoys playing with various codes.
      My Computer


  10. Posts : 18,034
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #10

    Hello @ricardobohner,

    ricardobohner said:
    Unless I got something wrong here you don't need powershell or VBS at all.
    Thanks for the reply, it is appreciated.

    I must admit that I prefer using == instead of EQU, but that is just personal preference.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 11:30.
Find Us




Windows 10 Forums