0

I found a couple of answers here which break at the space between Powershell and scripts.

Sub Test()
    Dim scriptlocation As String
    scriptlocation = "C:\REDACTED\Powershell Scripts\ConmonInfoFinder-V7.ps1"

    Dim x As Variant
    x = Shell("""POWERSHELL.exe"" ""C:\REDACTED\Powershell Scripts\ConmonInfoFinder-V7.ps1""", vbNormalFocus)
End Sub

Or

Sub Test()
    Dim scriptlocation As String
    scriptlocation = "C:\REDACTED\Powershell Scripts\Powershell Scripts\ConmonInfoFinder-V7.ps1"

    strCommand = "Powershell -File" & scriptlocation
    Set WshShell = CreateObject("WScript.Shell")
    Set WshShellExec = WshShell.Exec(strCommand)
End Sub

Both of these open Powershell but they won't run the script. The first one dies at the space in between Powershell Scripts and the second one flashes the windows with no error, but isn't running the script.

EDIT: I made some changes.

Sub Test()
    Dim scriptlocation As String
    scriptlocation = "C:\REDACTED\Powershell Scripts\trial.ps1"

    strCommand = "Powershell -NoExit -File """ & scriptlocation & """"
    Set WshShell = CreateObject("WScript.Shell")
    Set WshShellExec = WshShell.Exec(strCommand)

The window still flashes up and then goes away. I made a sample script of just Hello World to make sure it wasn't anything weird in my script and I have the same result.

EDIT:

It is now fixed.

Sub Test2()
    Dim scriptlocation As String
    scriptlocation = "C:\REDACTED\Powershell Scripts\ConmonInfoFinder-V7.ps1"

    strCommand = "Powershell -ExecutionPolicy Bypass -NoExit -File """ & scriptlocation & """"
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Exec (strCommand)
End Sub

2 Answers 2

3
strCommand = "Powershell -File" & scriptlocation

should be

strCommand = "Powershell -File """ & scriptlocation & """"

Use the -noexit flag if you want to keep the window open so you can see what's going on.

Sign up to request clarification or add additional context in comments.

2 Comments

I made those changes and am still having the same issue. Editing main post now
Nevermind, the issue was that ExecutionPolicy Bypass was not set. The line that fixed it was strCommand = "Powershell -ExecutionPolicy Bypass -NoExit -File """ & scriptlocation & """" Yours was the almost answer lol
1

Execution Policy is not set so the script won't run in default ExecutionPolicy settings command should be:

strCommand = "powershell -ep Bypass -F " & scriptlocation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.