Call the mage using Powershell 2.0 and call the operator (i.e. &)

I would like to use Powershell 2.0 for a script to create an application manifest using the Microsoft Manifest Generation and Editing tool (mage). In particular, I would like to be able to pass dynamically set parameter values ​​to the mage command (for example, read from xml or another source).

Although I can accomplish this with invoke-expression, I would prefer to avoid it, as it is considered less secure (ie vulnerable to "PowerShell injection").

Here is what I know.

This succeeds with the message "application.exe.manifest created successfully":

& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -New Application 

This does not work with the message "The first argument must be one of the following: -New, -Update, -Sign" (which is a magician, not powershell, with an error message):

 $params = "-New Application" & "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params 

How do I pass the value of $ params to the mage command so that it is successfully interpreted by the magician?

+6
powershell clickonce mage
source share
2 answers

Easy with $params defined as an array, one parameter for an array element:

 # define $params as an array $params = "-New", "Application" # and pass this array in, just as you tried before & "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params 

The same with $params dynamically built in a few steps:

 $params = @() ... $params += '-New' ... $params += 'Application' ... & "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params 

This method is often skipped, but it is very convenient, it allows you to easily dynamically create complex command lines for your own applications.

PS And we don’t need to take care of the spaces in the parameter values, we use the values ​​in the parameter array as they are, no additional is required. "

+6
source share

Process start

There are more ways to do this. First use the Start-Process :

 $p = '-h 3 google.com' start-process tracert -arg $p 

A new window will appear. If you want to start the process inside the console, just start it with -NoNewWindow

 $p = '-h 3 google.com' start-process tracert -arg $p -nonew $params = "-New Application" start-process "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -arg $params -nonew 

Invoke expression

Invoke-Expression can also help. But this is complicated because you have spaces in the path to the executable. This works because there is no place in the path:

 $p = '-h 3 google.com' invoke-expression "tracert $p" 

But if there is a space, you need to use & inside:

 $params = "-New Application" Invoke-Expression "& ""C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params" 

Note that the "& ""C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params" expands to this:

 & "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -New Application 

What did you want. But if there is a space in one of the parameters again, then again .. you need to quote it:

 $file1 = 'c:\test path\file1.txt' $file2 = 'c:\test path\file2.txt' $params = """$file1"" ""$file2""" Invoke-Expression "& someexecutable $params" 

The analysis is rather complicated: |

+2
source share

All Articles