Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>Invoke-Expression</code>, also aliased as <code>iex</code>. The following will work on your examples #2 and #3:</p> <pre><code>iex $command </code></pre> <p>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:</p> <pre><code>$command = 'C:\somepath\someexe.exe somearg' iex $command </code></pre> <p>However, if the the exe is in quotes, you need the help of <code>&amp;</code> to get it running, as in this example, as run from the commandline:</p> <pre><code>&gt;&gt; &amp;"C:\Program Files\Some Product\SomeExe.exe" "C:\some other path\file.ext" </code></pre> <p>And then in the script:</p> <pre><code>$command = '"C:\Program Files\Some Product\SomeExe.exe" "C:\some other path\file.ext"' iex "&amp; $command" </code></pre> <p>Likely, you could handle nearly all cases by detecting if the first character of the command string is <code>"</code>, like in this naive implementation:</p> <pre><code>function myeval($command) { if ($command[0] -eq '"') { iex "&amp; $command" } else { iex $command } } </code></pre> <p>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 <code>try{}catch{}</code>, perhaps for specific exception types/messages, or examine the command string.</p> <p>If you always receive absolute paths instead of relative paths, you shouldn't have many special cases, if any, outside of the 2 above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload