Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

How Do You Execute An Arbitrary Native Command From A String PDF

This document discusses executing an arbitrary native command from a string in PowerShell. It provides examples of using Invoke-Expression to run commands containing spaces, quotes, and other special characters. It also notes that prepending commands with '&' may be necessary if they are not native PowerShell commands.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

How Do You Execute An Arbitrary Native Command From A String PDF

This document discusses executing an arbitrary native command from a string in PowerShell. It provides examples of using Invoke-Expression to run commands containing spaces, quotes, and other special characters. It also notes that prepending commands with '&' may be necessary if they are not native PowerShell commands.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Home

PUBLIC

Stack Overflow

Tags
How do you execute an arbitrary native Ask Question

Users
command from a string?
Jobs

I can express my need with the following


TEAMS
scenario: Write a function that accepts a
Create Team 169 string to be run as a native command.

It's not too far fetched of an idea: if you're


interfacing with other command-line utilities
from elsewhere in the company that supply
you with a command to run verbatim.
47
Because you don't control the command,
you need to accept any valid command as
input. These are the main hiccups I've
been unable to easily overcome:

1. The command might execute a


program living in a path with a space
in it:
$command = '"C:\Program Files\TheProg\Runit.exe" Hello';

2. The command may have parameters


with spaces in them:
$command = 'echo "hello world!"';

3. The command might have both single


and double ticks:
$command = "echo `"it`'s`"";

Is there any clean way of accomplishing


this? I've only been able to devise lavish
and ugly workarounds, but for a scripting
language I feel like this should be dead
simple.

scripting powershell

edited Apr 27 '17 at 11:39


SteveC
6,421 18 69 121

asked Jun 14 '11 at 0:54


Johnny Kauffman
1,678 3 13 21

3 Answers

Invoke-Expression , also aliased as iex .


The following will work on your examples
255 #2 and #3:

iex $command
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
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"

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.

edited Dec 13 at 11:50


dubs
3,134 3 13 28

answered Jun 14 '11 at 1:00


Joel B Fant
21k 3 58 64

2 Aliasing is great. Remember, if you move


to another machine or send that script to
someone else that alias will probably not
be setup. Prefer the full names of the
PowerShell functions. – Doug Finke Jun
14 '11 at 1:17

1 @Doug: Most of the time I do that or use


the built-in aliases (especially for brevity
on the commandline). The eval things
is half-joking because that's what it is
called in so many other scripting
languages, and this isn't the first
question I've seen where someone had
no idea about invoke-expression . And
the OP's case sounds like in-house script
only. – Joel B Fant Jun 14 '11 at 1:25

I've tried this, but it does not work with:


$command = '"C:\Program
By using our site, you acknowledge that you have read and
Files\Windows understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Media
Player\mplayer2.exe"
"H:\Audio\Music\Stevie Wonder\Stevie
Wonder - Superstition.mp3"'
– Johnny Kauffman Jun 14 '11 at 4:40

3 You may have to put an "&" or "." sign


before the actual command if it isn't
powershell-native, e.g. Invoke-
Expression "& $command"
– Torbjörn Bergstedt Jun 14 '11 at 11:09

Right you are. I've updated my answer.


– Joel B Fant Jun 14 '11 at 13:53

Please also see this Microsoft Connect


report on essentially, how blummin' difficult
16 it is to use PowerShell to run shell
commands (oh, the irony).

http://connect.microsoft.com/PowerShell/fe
edback/details/376207/

They suggest using --% as a way to force


PowerShell to stop trying to interpret the
text to the right.

For example:

MSBuild /t:Publish --% /p:TargetDatabaseName


Source=.\;Integrated Security=True" /p:SqlPublishProfilePath
Database.sqlproj

answered Feb 18 '14 at 21:32


Luke Puplett
19.9k 23 126 187

Ah, and Microsoft broke the internet and


the link isn't valid anymore.
– Johan Boulé May 24 at 10:11

Above link is on archive.org at


web.archive.org/web/20131122050220/ht
tp://… – mwfearnley Aug 28 at 9:24

 

The accepted answer wasn't working for


me when trying to parse the registry for
4 uninstall strings, and execute them. Turns
out I didn't need the call to Invoke-
Expression after all.

I finally came across this nice template for


seeing how to execute uninstall strings:

$path = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$app = 'MyApp'
$apps= @{}
Get-ChildItem $path |
Where-Object -FilterScript {$_.getvalue
ForEach-Object -process {$apps.Set_Item
$_.getvalue('UninstallString'),
By using our site, you acknowledge that you $_.getvalue('DisplayName'))
have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
}
foreach ($uninstall_string in $apps.GetEnumerator
$uninstall_app, $uninstall_arg = $uninstall_string
& $uninstall_app $uninstall_arg
}

This works for me, namely because $app


is an in house application that I know will
only have two arguments. For more
complex uninstall strings you may want to
use the join operator. Also, I just used a
hash-map, but really, you'd probably want
to use an array.

Also, if you do have multiple versions of the


same application installed, this uninstaller
will cycle through them all at once, which
confuses MsiExec.exe , so there's that too.

edited Sep 11 '12 at 15:44

answered Sep 11 '12 at 14:40


Droogans
4,652 2 29 57

 

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

You might also like