Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run powershell script through excel VBA

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!

like image 756
Meghan Avatar asked Jul 11 '18 19:07

Meghan


1 Answers

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) 
like image 119
Tim Williams Avatar answered Oct 16 '22 21:10

Tim Williams