DX Unified Infrastructure Management

 View Only
  • 1.  LUA unable to run powershell command

    Posted Aug 28, 2020 03:58 AM
    Hi to all,

    can you please help me with this issue.

    My intention is to use rsp probe to check (with lua script) if a file exists on system, the version of file and when it was last modified. I have also tried to use dirscan but it doesn't provide all informations and tried to use rsp with WMI but it doesn't read (load) any instance in CIM_DataFile class My plan was to use powershell but i stumbled on problem that it is just not possible to run powershell command from LUA. I have tried few different approaches: 

    1.

    function tprint (t, s)
    for k, v in pairs(t) do
    local kfmt = '["' .. tostring(k) ..'"]'
    if type(k) ~= 'string' then
    kfmt = '[' .. k .. ']'
    end
    local vfmt = '"'.. tostring(v) ..'"'
    if type(v) == 'table' then
    tprint(v, (s or '')..kfmt)
    else
    if type(v) ~= 'string' then
    vfmt = tostring(v)
    end
    print((s or '')..kfmt..' = '..vfmt)
    end
    end
    end
    a=action.command ("powershell.exe -Command (Get-Item 'C:\Windows\System32\notepad.exe').VersionInfo.FileVersion")
    tprint(a)


    2.

    function cmdout(cmd)
    local f = io.popen(cmd)
    local lout = f:read("*a")
    f:close()
    return lout
    end

    cmd = "powershell.exe -Command '(Get-Item 'C:\Windows\System32\notepad.exe').VersionInfo.FileVersion'"
    lout = cmdout(cmd)
    print(lout)

    3.

    n = os.tmpname ()
    os.execute ("powershell.exe -Command "(Get-Item 'C:\Windows\System32\notepad.exe').VersionInfo.FileVersion" > " .. n)
    for line in io.lines (n) do
    print (line)
    end
    os.remove (n)

    Thank you


  • 2.  RE: LUA unable to run powershell command

    Broadcom Employee
    Posted Aug 28, 2020 09:20 AM

    the command function has never worked well with spaces in it.
    I would suggest try putting your commands into a bat file or script file and calling that to see if that works.



    ------------------------------
    Gene Howard
    Principal Support Engineer
    Broadcom
    ------------------------------



  • 3.  RE: LUA unable to run powershell command

    Posted Aug 28, 2020 10:25 AM
    Couple things about the code:

    Don't make assumptions about the execution environment - when your powershell script is run it will be run by whatever user started the Nimbus Watcher service - local SYSTEM by default. That might have inherited a completely different environment they you have as an interactive user.

    You user the backslash character in your code - in Lua, '\' is an escape character which means to take the character following and interpret it's meaning: https://www.lua.org/pil/2.4.html

    In order to use '\' in a string as a character you need to double it '\\'

    Putting things into a file and then invoking the file does insulate you from the affects of passing the command to a shell.

    Also note that this command essentially takes the text "cmd /c" and appends your command to it. You can simulate the behavior somewhat by doing the same thing - It is important to pay attention to all the things that cmd does to the argument of /c because the newer the version of Windows you are on the weirder it gets.





  • 4.  RE: LUA unable to run powershell command

    Posted Aug 28, 2020 10:51 AM
    Thank you for the replies i have a comment on both replies. I have already tried to put command into the batch file, but when i run that bat file in LUA i got for output the exact command as it was in bat file.

    For example: if i have in bat file echo "Hello World" output will not be Hello World but C:\<path_to_file>\echo "Hello World"...

    I also tried with \\ but it doesn't work. You can try it, that's why i used notepad.exe for example. In real problem it is different file.



  • 5.  RE: LUA unable to run powershell command

    Posted Aug 28, 2020 01:49 PM
    Please post the code you are trying to currently test with. There is no sense in trying to debug what you are not currently using.

    And as an example the code:

    n = os.tmpname ()
    os.execute ([[powershell.exe -Command "(Get-Item 'C:\\Windows\\System32\\notepad.exe').VersionInfo.FileVersion" > ]] .. n)
    for line in io.lines (n) do
    print (line)
    end

    results in 

    ----------- Executing script at 8/28/2020 12:43:07 PM ----------

    6.3.9600.16384 (winblue_rtm.130821-1623)


    You can simplify this script with:

    for line in io.popen([[ powershell.exe -Command "(Get-Item 'C:\\Windows\\System32\\notepad.exe').VersionInfo.FileVersion" ]]):lines()
    do
    print (line)
    end






  • 6.  RE: LUA unable to run powershell command

    Posted Aug 31, 2020 07:46 AM
    Hello Garin,

    thank you for the example, I have tried it inside nas lua editor with no sucess. There is no output. 

    I have tried to upload screenshot but that also doesn't work :(

    Any other suggestion?


  • 7.  RE: LUA unable to run powershell command
    Best Answer

    Posted Aug 31, 2020 08:09 AM
    Perhaps try to add the powershell path?
    When launching a script via UIM the path is not the same as when you execute the command/script manually


  • 8.  RE: LUA unable to run powershell command

    Posted Aug 31, 2020 08:19 AM
    Thx Luc, is working :) Now we start with hard part :)