【问题标题】:Shell execute in luaShell 在 lua 中执行
【发布时间】:2014-05-13 11:59:33
【问题描述】:

powershell 脚本

    Set-ExecutionPolicy Unrestricted
##    NEEDED FOR IIS CMDLETS
Import-Module WebAdministration

##    CREATE FTP SITE AND SET C:\inetpub\ftproot AS HOME DIRECTORY
New-WebFtpSite -Name "test" -Port "21" -Force
cmd /c \Windows\System32\inetsrv\appcmd set SITE "test" "-virtualDirectoryDefaults.physicalPath:C:\inetpub\ftproot"

##    SET PERMISSIONS

     ## Allow SSL connections 
Set-ItemProperty "IIS:\Sites\test" -Name ftpServer.security.ssl.controlChannelPolicy -Value 0
Set-ItemProperty "IIS:\Sites\test" -Name ftpServer.security.ssl.dataChannelPolicy -Value 0

     ## Enable Basic Authentication
Set-ItemProperty "IIS:\Sites\test" -Name ftpServer.security.authentication.basicAuthentication.enabled -Value $true
## Set USer Isolation
 Set-ItemProperty "IIS:\Sites\test" -Name ftpserver.userisolation.mode -Value 3

#Set-ItemProperty "IIS:\Sites\test" -Name ftpServer.security.userIsolation. -Value $true

     ## Give Authorization to Administrators and grant "read"/"write" privileges
Add-WebConfiguration "/system.ftpServer/security/authorization" -value @{accessType="Allow";roles="";permissions="Read,Write";users="*"} -PSPath IIS:\ -location "test"
## Give Authorization to All Users 
#appcmd set config %ftpsite% /section:system.ftpserver/security/authorization /+[accessType='Allow',permissions='Read,Write',roles='',users='*'] /commit:apphost 

     ## Restart the FTP site for all changes to take effect
Restart-WebItem "IIS:\Sites\test"

我想用 lua 运行它

我制作了那个脚本

    function Create_iis()
    tl1="Set-ExecutionPolicy Unrestricted"
    tl2="Import-Module WebAdministration"
    tl3="New-WebFtpSite -Name \"test\" -Port \"21\" -Force" 
    tl4="cmd \/c \\Windows\\System32\\inetsrv\\appcmd set SITE \"test\" \"-virtualDirectoryDefaults.physicalPath:C:\\inetpub\\ftproot\""
    tl5="Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpServer.security.ssl.controlChannelPolicy -Value 0"
    tl6="Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpServer.security.ssl.dataChannelPolicy -Value 0"
    tl7="Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpServer.security.authentication.basicAuthentication.enabled -Value $true"
    tl8="Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpserver.userisolation.mode -Value 3"
    tl9="Add-WebConfiguration \"\/system.ftpServer\/security\/authorization\" -value \@\{accessType=\"Allow\";roles=\"\";permissions=\"Read\,Write\";users=\"*\"} -PSPath IIS:\ -location \"test\"" 
    tl10="Restart-WebItem \"IIS:\\Sites\\test\""

    file = io.open("c:\\testiis.ps1","w");
    file:write(tl1.."\n"..tl2.."\n"..tl3.."\n"..tl4.."\n"..tl5.."\n"..tl6.."\n"..tl7.."\n"..tl8.."\n"..tl9.."\n"..tl10.."\n"..);
    file:close("c:\\testiis.ps1");
    result = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe", "open", "c:\\testiis.ps1", "", SW_HIDE,true);
    if result ~=0 then 
        Dialog.Message("Error", "Error ", MB_OK, MB_ICONSTOP, MB_DEFBUTTON1);
    end
end

工作顺利

但是如果返回错误,我需要检查此脚本中的每一行然后退出 并给我造成错误的那一行

所以我制作了愚蠢的脚本

    function Create_iis()
result1 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Set-ExecutionPolicy Unrestricted")
result2 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Import-Module WebAdministration")
result3 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","New-WebFtpSite -Name \"test\" -Port \"21\" -Force" )
result4 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","cmd \/c \\Windows\\System32\\inetsrv\\appcmd set SITE \"test\" \"-virtualDirectoryDefaults.physicalPath:C:\\inetpub\\ftproot\"")
result5 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpServer.security.ssl.controlChannelPolicy -Value 0")
result6 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpServer.security.ssl.dataChannelPolicy -Value 0")
result7 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpServer.security.authentication.basicAuthentication.enabled -Value $true")
result8 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Set-ItemProperty \"IIS:\\Sites\\test\" -Name ftpserver.userisolation.mode -Value 3")
result9 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Add-WebConfiguration \"\/system.ftpServer\/security\/authorization\" -value \@\{accessType=\"Allow\";roles=\"\";permissions=\"Read\,Write\";users=\"*\"} -PSPath IIS:\ -location \"test\"" )
result10 = Shell.Execute("C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe","open","Restart-WebItem \"IIS:\\Sites\\test\"")

end

并检查每个结果

但有一些命令依赖于每个命令,因此会出错

这不可能吗?

抱歉英语不好

提前致谢

【问题讨论】:

  • 什么是Shell.Execute?我的意思是这个函数是从哪里来的?
  • 它是设置工厂 lua 中的内置函数
  • 您围绕现有的功能性 PowerShell 脚本编写 Lua 包装器的目的是什么?
  • 我想在没有附加powershell的情况下实现powershell命令,因为它包含其他秘密
  • 如果我只使用 vanilla Lua,这里的解决方案将是 io:popen 来执行脚本并从其标准输出中读取,但我猜这不可用?

标签: powershell command-line lua shellexecute


【解决方案1】:

Lua 中可以使用io.popen 调用命令:

local file = assert(io.popen('/bin/ls -la', 'r'))
local output = file:read('*all')
file:close()
print(output) -- > Prints the output of the command.

来源:https://stackoverflow.com/a/5243210/1069083

【讨论】:

    猜你喜欢
    • 2017-11-28
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多