2

This is most likely a question related to the way I'm calling the Powershell instance from the VBA code, but I could not find the relevant information on StackOverflow. This Microsoft forum post made a very simple suggestion but I couldn't make it work. Others had similar suggestions but I can't make it run in my environment.

My goal is to run a specific Powershell script when I receive a mail in Outlook. This could probably be done easier by making the actual script in VBA to begin with, but since I'm already trying to learn Powershell first and foremost I would rather not go down the VBA road just yet. The Powershell script itself can run and works fine on its own, but VBA in the ThisOutlookSession can't seem to call it (read: I'm making a mistake somewhere that makes VBA unable to call the Powershell script). In order to make sure it's not the Powershell script I made a simple test script that only displays a pop-up window saying 'operation completed'.

This is the VBA code:

Sub test()
'Call Powershell and run scripts
    Call Shell("powershell -noexit -file ""c:\users\me\desktop\test.ps1")
End Sub

This runs and opens a Powershell instance but says that "running scripts are disabled on your system", ergo I need to use the -ExecutionPolicy parameter. I changed the script to the following:

Sub test()
'Call Powershell and run scripts
    Call Shell("powershell -noexit -ExecutionPolicy Bypass -file ""c:\users\me\desktop\test.ps1")
End Sub

This makes the VBA code unable to run with the following error:

Invalid Procedure Call

This error occurs regardless of where I put the -ExecutionPolicy (before -Noexit, after -File, etc.). I have enabled macros from the Trust Center settings and restarted Outlook. I tried fiddling around with the quotation marks, and by adding some other parameters (-NoProfile etc.) to see if that worked but it still gives the same result. I'm fairly sure that it is the formatting of the -ExecutionPolicy, or the order of it that makes it unable to call it.

Any ideas are appreciated!

Tanaka Saito
  • 943
  • 1
  • 17
  • 40
  • please check this http://stackoverflow.com/questions/4037939/powershell-says-execution-of-scripts-is-disabled-on-this-system – Maddy May 03 '17 at 11:42
  • Thank you, however that post gives answers telling OP that (s)he should use -ExecutionPolicy Bypass when calling the Powershell instance. This is what I'm describing in the above post but I'm still getting the 'invalid procedure call' error. – Tanaka Saito May 03 '17 at 11:48
  • 1
    Are you sure your parameters for the call are correct? I'm not that familiar with VBA but it looks like you insert a `"` but don't end the path to the script with one? Does this work (with the error about execution permissions) if you just remove the execution policy parameter? – Seth May 03 '17 at 12:02
  • Yes, I was trying to describe just that. The first section (without the -ExecutionPolicy Bypass) works and starts a Powershell instance. The first thing that is displayed there is the "running scripts are disabled on your system". If I in that window run Get-ExecutionPolicy it is set to Restricted, so I need to call the Powershell instance with elevated privileges from the VBA code. I'm sure that it's the parameter call in the VBA stuff that's incorrect, but I can't figure out what :) – Tanaka Saito May 03 '17 at 12:04
  • 1
    Check if `RemoteSigned` as supplied execution policy would work. Or plain sign your file. I expect that you're trying to do what hackers do in order to exploit the system into running unsigned (read any) code. – Vesper May 03 '17 at 12:40
  • Holy moly, that worked, thank you so much! I assume that VBA doesn't allow for calling the -ExecutionPolicy parameter with Bypass, or is there another reason that this is prohibited? – Tanaka Saito May 03 '17 at 12:42
  • If you add the RemoteSigned stuff as an answer I can mark it as accepted by the way :) – Tanaka Saito May 03 '17 at 13:27

1 Answers1

0

To make it easier for future viewers: the solution was to instead of using -ExecutionPolicy Bypass which seems to be restricted (for good reasons) I instead used -ExecutionPolicy RemoteSigned and that did the trick. Thanks to @Vesper for providing the solution.

Tanaka Saito
  • 943
  • 1
  • 17
  • 40