Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke mage using Powershell 2.0 and invoke operator (i.e. &)

I would like to use Powershell 2.0 to script the creation of an application manifest using Microsoft's Manifest Generation and Editing tool (mage). Specifically, I would like to be able to pass dynamically specified parameter values to the mage command (e.g. read from xml or some other source).

Although I can accomplish this using invoke-expression, I would prefer to avoid it is regarded as a less secure option (i.e. vulnerable to "powershell injection attacks").

Here is what I know.

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

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

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

$params = "-New Application"

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

How can I pass the $params value to the mage command so it is successfully interpreted by mage?

like image 282
Adam Flynn Avatar asked Nov 24 '10 19:11

Adam Flynn


People also ask

How do I check PowerShell v2?

Details. Open "PowerShell". Enter "Get-WindowsFeature | Where Name -eq PowerShell-v2". If "Installed State" is "Installed", this is a finding.

What is IEX PowerShell?

iex is an alias for Invoke-Expression . Here the two backticks don't make any difference, but just obfuscates the command a little. iex executes a string as an expression, even from pipe. Here Start-Process is a cmdlet that starts processes.

What does >> mean in PowerShell?

That means that the console is waiting for more input. You've probably got a missing } somewhere.

What is $using in PowerShell?

Beginning in PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command. The syntax of Using is as follows: $Using:<VariableName> In the following example, the $ps variable is created in the local session, but is used in the session in which the command runs.


1 Answers

It is easy with $params defined as an array, one parameter per array item:

# 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

Ditto 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 way is often overlooked but it is very handy, it allows easy dynamic composition of complex command lines for native applications.

P.S. And we do not have to care of spaces in parameter values, we use values in the parameter array as they are, no extra " needed.

like image 75
Roman Kuzmin Avatar answered Sep 23 '22 17:09

Roman Kuzmin