Skip to content

Instantly share code, notes, and snippets.

@codeartery
Last active July 21, 2024 05:32
Show Gist options
  • Save codeartery/9c0317129b64ce63733ba692a7211dc1 to your computer and use it in GitHub Desktop.
Save codeartery/9c0317129b64ce63733ba692a7211dc1 to your computer and use it in GitHub Desktop.
Run a command prompt command and get its output in VBScript.
Function CmdOut( pCmd )
REM@description
' Run a command prompt command and get its output.
REM@params
' pCmd <string> - A command prompt command.
REM@returns
' CmdOut <string> - The output of the command.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
' Function CmdOut(p):Dim w,e,r,o:Set w=CreateObject("WScript.Shell"):Set e=w.Exec("Cmd.exe"):e.StdIn.WriteLine p&" 2>&1":e.StdIn.Close:While(InStr(e.StdOut.ReadLine,">"&p)=0)::Wend:Do:o=e.StdOut.ReadLine:If(e.StdOut.AtEndOfStream)Then:Exit Do:Else:r=r&o&vbLf:End If:Loop:CmdOut=r:End Function
Dim oWss, oExe, Return, Output
Set oWss = CreateObject( "WScript.Shell" )
Set oExe = oWss.Exec( "Cmd.exe" )
Call oExe.StdIn.WriteLine( pCmd & " 2>&1" )
Call oExe.StdIn.Close()
While( InStr( oExe.StdOut.ReadLine, ">" & pCmd ) = 0 ) :: Wend
Do : Output = oExe.StdOut.ReadLine()
If( oExe.StdOut.AtEndOfStream )Then
Exit Do
Else
Return = Return & Output & vbLf
End If
Loop
CmdOut = Return
End Function
REM@usage
' Put the full or mini class/sub/function in your script to use.
Function CmdOut(p):Dim w,e,r,o:Set w=CreateObject("WScript.Shell"):Set e=w.Exec("Cmd.exe"):e.StdIn.WriteLine p&" 2>&1":e.StdIn.Close:While(InStr(e.StdOut.ReadLine,">"&p)=0)::Wend:Do:o=e.StdOut.ReadLine:If(e.StdOut.AtEndOfStream)Then:Exit Do:Else:r=r&o&vbLf:End If:Loop:CmdOut=r:End Function
' returns the result of whatever command you run
Dim Result
Result = CmdOut( "ECHO Hello, world!" )
MsgBox Result
' if you run with cscript instead of wscript you can see the full output when using wscript.echo because msgbox has a character limit
Dim IpConfig
IpConfig = CmdOut( "ipconfig /all" )
WScript.Echo IpConfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment