Search code examples
windowspowershellluaexecute

execute powershell commands with Lua


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.

Methods like os.execute() or io.popen() use the standard command line from windows and not Powershell.

Is there a way to use Powershell with lua?


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")


Solution

  • -- 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()