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:
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!