I'm not the best at this, but it appears there are (at least) two ways to call a PowerShell script from vba one is to use .Run and the other is to use .Exec
RUN let's you add a parameter called waitOnReturn
EXEC let's you read the output of the script via .StdOut.ReadAll
But I need to do both at the same time.
Effectively, i'll be looping through a list of files, running the script multiple times - the script itself will have a delay that will be indeterminate - so i can't just use a normal VBA wait for xx seconds.
So, my question is - how do I both wait for the script to finish AND read the output of the script?
Here's an example of .Run (that i found on this forum) using the waitOnReturn
Here's an example of using .Exec and .StdOut
RUN let's you add a parameter called waitOnReturn
EXEC let's you read the output of the script via .StdOut.ReadAll
But I need to do both at the same time.
Effectively, i'll be looping through a list of files, running the script multiple times - the script itself will have a delay that will be indeterminate - so i can't just use a normal VBA wait for xx seconds.
So, my question is - how do I both wait for the script to finish AND read the output of the script?
Here's an example of .Run (that i found on this forum) using the waitOnReturn
Code:
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Integer
wsh.Run "POWERSHELL.EXE " & "\\PathToYourPowerShellScript\PowerShellScript.ps1", windowStyle, waitOnReturn
MsgBox "Finished"
Code:
strCommand = "Powershell -file ""C:\PowerShellScriptName.ps1"""
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
strOutput = WshShellExec.StdOut.ReadAll
Sheets("Sheet1").Range("A1").Value = strOutput