I need execute a command line in a Visual Basic Script

后端 未结 3 617
小蘑菇
小蘑菇 2020-12-16 01:43

I need to execute the command \"ver\" in my vbs to see the version of my Operating System, and i don\'t know how make it.

I tried this, but dont work:



        
3条回答
  •  有刺的猬
    2020-12-16 01:45

    There is a way to do this without having to write output to a file.

    For example, suppose you wanted to capture the text of a directory listing. (There would be lots of better ways to get it than this, but I'm just using a simple example.)

    With the function below in your VBScript, you could enter:

    thisDir = getCommandOutput("cmd /c dir c:")
    

    And when the above line is executed, the variable "thisDir" would contain the output from the DIR command.

    Note that some commands you want output from will require you to pass them through the command shell (the "cmd /c" part of the above), while others may work fine if you run them directly without the shell. Try it without the command shell. If it fails, try it with the command shell.

    '
    ' Capture the results of a command line execution and
    ' return them to the caller.
    '
    Function getCommandOutput(theCommand)
    
        Dim objShell, objCmdExec
        Set objShell = CreateObject("WScript.Shell")
        Set objCmdExec = objshell.exec(thecommand)
        getCommandOutput = objCmdExec.StdOut.ReadAll
    
    end Function
    

提交回复
热议问题