Pass Excel Cell parameters to PowerShell script

rb33339

New Member
Joined
Feb 10, 2018
Messages
1
I’m fairly new to Excel macros. I have an Excel 2016 form that I’ve created for some data entry. I want to be able to run a PowerShell script when a particular cell is selected. My issue is I can run a PowerShell script but I can’t figure out how to pass 2 parameters to the PowerShell script in the macro. The parameters will come from two separate cells in the worksheet. I’ve been trying to use the Windows Script Hosts Exec method, as outlined here https://superuser.com/questions/724981/call-powershell-script-from-excel-macro but with no success. All I get is an empty Excel dialogue box

Is this a supported function or do I have to use an alternate method? I was hoping to use an Excel dialogue to return the result, but at the end of the day if I have to use PowerShell to return the result, fine; as long as I can pass it the parameters via the Excel cells.

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Did you ever figure this out? I am trying to create a button that runs a powershell command in one of my resulting cells
 
Upvote 0
Try this example:
Code:
Public Sub Test()
    
    Dim PScommand As String
    
    PScommand = "-NoExit -Command " & Q("& {get-process}")
    
    Debug.Print PScommand
    Shell "PowerShell " & PScommand
        
End Sub

Private Function Q(t As String) As String
    Q = Chr(34) & t & Chr(34)
End Function
If A2 contains get-process then the code would be:
Code:
    PScommand = "-NoExit -Command " & Q("& {" & ActiveSheet.Range("A2").Text & "}")
 
Upvote 0
Thanks I figured out a different way however my antivirus instantly shuts down excel due to exploit protection - I cannot make an exception all I can do is just switch it off completely which I would prefer not to do
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,610
Members
449,174
Latest member
ExcelfromGermany

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top