Search code examples
batch-filevbscriptcmdpsexec

Sending input to a cmd window using vbscript


I am trying to automate my process. I need to ask the user for the computer name ex: SH010123 store that as a variable. start cmd prompt. type psexec\\"computername" cmd then I need to type net use t: \\network\path password /user:domain\username and finally type \\network\path\"my file.bat"

I cant figure out how to use vbscript to send those inputs to a cmd window.


Solution

  • Although the solution may be implemented bypassing sending input to a cmd window, here is an example how to send the command to cmd and get back some output back:

    Dim oShell, oExec
    Set oShell = CreateObject("WScript.Shell")
    Set oExec = oShell.exec("%comspec%")
    oExec.StdIn.Write "dir" & vbCrLf
    oExec.StdIn.Write "cd c:\" & vbCrLf
    oExec.StdIn.Write "dir" & vbCrLf
    oExec.StdIn.Write "exit" & vbCrLf
    ShowInNotepad oExec.StdOut.readall
    
    Sub ShowInNotepad(strTest)
        Dim TempPath
        With CreateObject("Scripting.FileSystemObject")
            TempPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%") & "\" & .GetTempName
            With .CreateTextFile(TempPath, True, True)
                .WriteLine(strTest)
                .Close
            End With
            CreateObject("WScript.Shell").Run "notepad.exe " & TempPath, 1, True
            .DeleteFile(TempPath)
        End With
    End Sub