none
Call Powershell from VBA RRS feed

  • Question

  • Hi

    I tried your Ed Wilsons script in vbscript:

    option explicit

    dim objShell

     

    Wscript.Echo "Starting"

    Set objShell = CreateObject("Wscript.shell")

    objShell.run("powershell -executionpolicy bypass -noexit -File D:\IT\PS1\HelloWorld.ps1")

    Wscript.Echo "Done"

     

    It worked fine.

     

    I tried to start Powershell from  Excel vba:

    Option Explicit

     

    Private Sub CommandButton1_Click()

    Dim sCommand As String, sOutput As String

    Dim oWshShell As Object, oWshShellExec As Object

     

    sCommand = "Powershell -executionpolicy bypass -noexit "

    Debug.Print sCommand

    Set oWshShell = CreateObject("WScript.Shell")

    Set oWshShellExec = oWshShell.Exec(sCommand)

    sOutput = oWshShellExec.StdOut.ReadAll

    MsgBox (sOutput)

    Set oWshShellExec = Nothing

    Set oWshShell = Nothing

    End Sub

     

    But it do not work.

    ProcMon shows that Powersheel starts and immediately stops.

    “-noexit” has no effect.

     

    How do I start Powershell with -noexit from Excel vba?

     

    Regards

     

    Jan Andersen

    Boelsvang 26

    DK-2970 Hoersholm

    Denmark

     

    Mobile:          +45 2022 1196

    E-mail:          janand@janand.dk

    www:             www.janand.dk

     

    • Moved by Bill_Stewart Monday, April 30, 2018 9:39 PM This is not MS Office development forum
    Wednesday, February 28, 2018 2:05 PM

All replies