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

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -