How do you execute an arbitrary native command from a string?

前端 未结 4 1459
小鲜肉
小鲜肉 2020-11-27 09:44

I can express my need with the following scenario: Write a function that accepts a string to be run as a native command.

It\'s not too far fetched

4条回答
  •  庸人自扰
    2020-11-27 10:08

    Invoke-Expression, also aliased as iex. The following will work on your examples #2 and #3:

    iex $command
    

    Some strings won't run as-is, such as your example #1 because the exe is in quotes. This will work as-is, because the contents of the string are exactly how you would run it straight from a Powershell command prompt:

    $command = 'C:\somepath\someexe.exe somearg'
    iex $command
    

    However, if the exe is in quotes, you need the help of & to get it running, as in this example, as run from the commandline:

    >> &"C:\Program Files\Some Product\SomeExe.exe" "C:\some other path\file.ext"
    

    And then in the script:

    $command = '"C:\Program Files\Some Product\SomeExe.exe" "C:\some other path\file.ext"'
    iex "& $command"
    

    Likely, you could handle nearly all cases by detecting if the first character of the command string is ", like in this naive implementation:

    function myeval($command) {
        if ($command[0] -eq '"') { iex "& $command" }
        else { iex $command }
    }
    

    But you may find some other cases that have to be invoked in a different way. In that case, you will need to either use try{}catch{}, perhaps for specific exception types/messages, or examine the command string.

    If you always receive absolute paths instead of relative paths, you shouldn't have many special cases, if any, outside of the 2 above.

提交回复
热议问题