Run a Powershell command (not script) from Excel VBA -


i've searched so, , can find plenty of examples of running powershell script vba, can't find examples of running simple command.

for example, works:

dim retval variant retval = shell("powershell ""c:\myscript.ps1""", vbnormalfocus) 

but not:

dim retval variant dim pscmd string  pscmd = "powershell " & _   chr(34) & "get-scheduledtask" & _   " -taskname " & chr(34) & "my task" & chr(34) & _   " -cimsession mylaptop" & chr(34) retval = shell(pscmd, vbnormalfocus)  debug.print pscmd 'pscmd = powershell "get-scheduledtask -taskname "my task" -cimsession mylaptop" 

i know write ps command file, execute script, , delete file, not seem elegant.

to run inline powershell command, you'll need use -command param , surround statement quotes. it'll easier if use single quotes within command.

try out:

pscmd = "powershell -command ""{get-scheduledtask -taskname 'my task' -cimsession mylaptop}""" 

Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -