none
Query command in Vbscript RRS feed

  • Question

  • Hi,

    Can anyone help me to correct the below Vbscript. I am able to run the command "systeminfo | find /I "System Boot Time" to get system boot time.But when I put the command in Vbscript its not giving proper output

    Dim strstatus
    Dim oWsh:Set oWsh = CreateObject("WScript.Shell")
    strstatus = "systeminfo | find /I "System Boot Time""
    oWsh.Run strstatus

    Wednesday, March 9, 2016 8:11 AM

Answers

  • Hi Shalton,

    This should do the trick:

    Set oWsh = CreateObject("WScript.Shell")
    strCommand = "%comspec% /c systeminfo | find /I ""System Boot Time"""
    Set objExecObject = oWsh.Exec(strCommand)
        Do While Not objExecObject.StdOut.AtEndOfStream
            strText = objExecObject.StdOut.ReadAll()
    Wscript.Echo strText
        Loop


    Regards,
    Salvador Manaois III
    MCSE MCSA CEH MCITP | Enterprise/Server Admin
    MCSA:Windows 7/2008
    http://www.badzmanaois.com

    Wednesday, March 9, 2016 8:36 AM
    Moderator
  • No need to shell and parse output. Just ask WMI. PowerShell:


    $os = Get-WmiObject Win32_OperatingSystem
    $os.ConvertToDateTime($os.LastBootUpTime)
    

    VBScript:


    Dim WMI, OSColl, OS, CIMDate
    Set WMI = GetObject("Winmgmts:root/CIMV2")
    Set OSColl = WMI.InstancesOf("Win32_OperatingSystem")
    For Each OS In OSColl
      CIMDate = OS.LastBootUpTime
      Exit For
    Next
    
    Dim DateTime
    Set DateTime = CreateObject("WbemScripting.SWbemDateTime")
    DateTime.Value = CIMDate
    
    WScript.Echo DateTime.GetVarDate
    

    This is a good example that shows how much simpler things are in PowerShell.


    -- Bill Stewart [Bill_Stewart]


    Wednesday, March 9, 2016 3:14 PM
    Moderator

All replies

  • Hi Shalton,

    This should do the trick:

    Set oWsh = CreateObject("WScript.Shell")
    strCommand = "%comspec% /c systeminfo | find /I ""System Boot Time"""
    Set objExecObject = oWsh.Exec(strCommand)
        Do While Not objExecObject.StdOut.AtEndOfStream
            strText = objExecObject.StdOut.ReadAll()
    Wscript.Echo strText
        Loop


    Regards,
    Salvador Manaois III
    MCSE MCSA CEH MCITP | Enterprise/Server Admin
    MCSA:Windows 7/2008
    http://www.badzmanaois.com

    Wednesday, March 9, 2016 8:36 AM
    Moderator
  • Thanks Much Salvador. The script works great
    Wednesday, March 9, 2016 9:12 AM
  • No need to shell and parse output. Just ask WMI. PowerShell:


    $os = Get-WmiObject Win32_OperatingSystem
    $os.ConvertToDateTime($os.LastBootUpTime)
    

    VBScript:


    Dim WMI, OSColl, OS, CIMDate
    Set WMI = GetObject("Winmgmts:root/CIMV2")
    Set OSColl = WMI.InstancesOf("Win32_OperatingSystem")
    For Each OS In OSColl
      CIMDate = OS.LastBootUpTime
      Exit For
    Next
    
    Dim DateTime
    Set DateTime = CreateObject("WbemScripting.SWbemDateTime")
    DateTime.Value = CIMDate
    
    WScript.Echo DateTime.GetVarDate
    

    This is a good example that shows how much simpler things are in PowerShell.


    -- Bill Stewart [Bill_Stewart]


    Wednesday, March 9, 2016 3:14 PM
    Moderator