7

I am trying to use an Excel VBA macro to launch a PowerShell script if a certain condition is met. I'm not getting any errors in the code when it runs and I am able to manually run the PowerShell script from the Windows Explorer window without errors. However, when I execute the below VBA code, the PowerShell does not run and I am not sure why.

Dim x as Variant
If rs.RecordCount = 0 Then
    x = Shell("POWERSHELL.exe " & "H:\MyFolder\MyFile.ps1", 1)
End If

I can't tell if something in the VBA code is wrong because I'm not getting any run time errors but the PowerShell script is not actually running

UPDATE: with the extra quotes I am able to see the error message now that's popping up in the cmd window but it is still having issues with the space even with the extra quotes. I have moved my script to a different file path that doesn't have spaces but I now seeing errors that running scripts are disabled. This seems like it is no longer a code based problem. thank you all!

Meghan
  • 115
  • 1
  • 1
  • 7
  • Is PS execution policy allowing PS1 scripts to run? Default is to block execution. Try adding the `-noexit` switch if the PS1 window immediately closes. – Mathieu Guindon Jul 11 '18 at 20:04
  • 2
    I tried and that did not help, I see the window pop open but then closes and the script isn't executed. I did the below: `x = Shell("POWERSHELL.exe -noexit" & "H:\Operations\REPORTS\Reports2018\Balance Sheet\SLmarginJE.ps1", 1)` – Meghan Jul 11 '18 at 20:16
  • You can [edit] your question with any additional relevant info / what you've tried. – Mathieu Guindon Jul 11 '18 at 20:17
  • 1
    `...-noexit" & "H:\Operations\...` you haven't left a space between these arguments – Tim Williams Jul 11 '18 at 20:18

1 Answers1

13

If your file path has spaces you need to put quotes around it.

x = Shell("POWERSHELL.exe -noexit " & _
           """H:\Operations\REPORTS\Reports2018\Balance Sheet\SLmarginJE.ps1""", 1) 
Tim Williams
  • 154,628
  • 8
  • 97
  • 125