问题
Good evening,
I have a program that I work with, that has an onboard lua compiler to allow for custom written actions.
Since the tool itself is very limited, especially if it goes for complex reactions over networks, I want to use Powershell over lua.
Sadly there isn't much to find about that (at least I didn't), and things like os.execute()
or io.popen()
use the standard command line from windows.
Does someone know a library or another way to use Powershell with lua.
What I already tried: I tried to write a command line script with the Powershell editor and run this script with os.execute, but it opens it as a textfile, it would be better to write the commands directly in lua but if there is no other way, executing a Powershell script directly would also be fine. (In Windows itself you can execute the script with right mouse "click/Execute with Powershell"
回答1:
-- You can generate PowerShell script at run-time
local script = [[
Write-Host "Hello, World!"
]]
-- Now create powershell process and feed your script to its stdin
local pipe = io.popen("powershell -command -", "w")
pipe:write(script)
pipe:close()
回答2:
Your description of the problem makes it sound like you're using a command such as os.execute("powershellscript.ps1")
, and that call invokes cmd.exe
with your string as the proposed command line. Normally, Windows will open a .PS1
file for editing; this was a deliberate decision for safety. Instead, try altering the os.execute()
command to explicitly call PS: os.execute("powershell.exe -file powershellscript.ps1")
. If you need to pass parameters to your script, enclose them in {}
. See https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help for more info on invoking PowerShell from the command line.
来源:https://stackoverflow.com/questions/41039879/execute-powershell-commands-with-lua