Run script from within Excel

Experts,

I received a solution to the following question that works perfectly. However, to get the results, a script must be run from a command prompt. Is there anyway to have this done from within Excel?

Previously asked question: Solution

Command lines to run script:
cd /d C:\Scripts\GetHandicap
powershell.exe -ExecutionPolicy Unrestricted -File GetHandicap.ps1

Thanks, Dale
Dale LoganConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
Hi

Try this

Private Sub CommandButton1_Click()

    Dim strProgramName As String
    Dim strArgument As String

    strProgramName = "C:\Scripts\GetHandicap\powershell.exe"
   
    strArgument = " -ExecutionPolicy Unrestricted -File GetHandicap.ps1"

    Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)

End Sub
To-run-from-excel.xlsm
Dale LoganConsultantAuthor Commented:
I get an error message "File not found." on the "Call Shell" line. I've checked for typos and don't find any. I have attached a screen shot of the file location. Again, I really don't know what's going on here since someone else created this for me.

Capture
Capture2
zalazarCommented:
The following code should accomplish this.
The code will first change the working directory and then execute the PowerShell command.
The output of the PowerShell will not be visible in the window.
The output will stored in a variable and then the data is processed and filled into column A and B but you can easily adjust that.

Sub CommandButton1_Click()
  Dim WshShell
  Set WshShell = CreateObject("WScript.Shell")

  sWorkingDir = "C:\Scripts\GetHandicap"
  sCommandLine = "powershell.exe -ExecutionPolicy Unrestricted -File GetHandicap.ps1"

  WshShell.CurrentDirectory = sWorkingDir
  Set objScriptExec = WshShell.Exec(sCommandLine)
  sResults = objScriptExec.StdOut.ReadAll
  arrResults1 = Split(sResults, Chr(10), -1, 1)
  For i = 0 To UBound(arrResults1)
    arrResults2 = Split(arrResults1(i), ";", -1, 1)
    If UBound(arrResults2) = 1 Then
      Range("A" & i + 1).Value = arrResults2(0)
      Range("B" & i + 1).Value = arrResults2(1)
    End If
  Next

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Dale LoganConsultantAuthor Commented:
zalazar,

Just so you know, this runs correctly. However, there is no way to know when the process has finished. The Command window stays open. Only when I close it, does the data populate in Excel. So, I just have to wait long enough before closing the window. Is there anyway for the process to know it has finished and then have it close the Command window?
zalazarCommented:
That's indeed not so easy then. I actually do not have the problem myself.
While looking at it, things could probably be done better and therefore I have written some Excel VBA code that does the same as the PowerShell script.
You can also specify the users in the Excel sheet now instead of a txt file.
I have attached an Excel workbook that you can use.

Within the workbook you have to fill in the correct parameters in column B.
From the script directory curl.exe is used.
The users column, start and end row have to be defined.
It's not a problem if you put in a higher number for "Users Row End" as empty cells are automatically skipped.
Hopefully this works without any problem and hopefully it also makes the process easier.
GetHandicap-v10.xlsb
Dale LoganConsultantAuthor Commented:
zalazar,

This is awesome. Thank you so much.

Dale
zalazarCommented:
You're most welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.