Question

I need to "translate" or rewrite a old batch file into powershell.

Command is an exe file. It looks roughly like:

dir1\dir2\XsltTransform.exe -i dir_1\dir_2\blah.xml -x dir_x\dir_y\something.xslt -o path1\path2\result.html

I am a bit confused on the -i, -x, and -o bit

Also some command even have -r, -p, -a, -b, -l as well.

I know they possibly mean option of some sort.

But I just can't seem to find on Google or StackOverflow.

Was it helpful?

Solution

Your example code has an EXE file. I am assuming you have access to the source-code from which the EXE was generated.

The -i -x -o are all input parameters. Somewhere within that EXE, there's some code that handles these parameters and stores them into variables.

-i dir_1\dir_2\blah.xml -x dir_x\dir_y\something.xslt -o path1\path2\result.html

Thus, dir_1\dir_2\blah.xml would (probably) get stored into some variable for further processing, and so on.

While reading this answer, read this as well. Keep the Microsoft TechNet and MSDN websites handy - it helps :)

To be able to launch a Powershell script with "-x -y"parameters, you need to define the following at the top of your script.

Basic Usage

Param(
  [string]$x,
  [string]$y
)

Here you'd call the script as script -x -y

This will accept two arguments -x and -y. Whatever you feed in is stored as a string (in this example), in variables $x $y.

If you want any other datatype simply change the [string] value.

Advanced Usage

  • You can also make a parameter have aliases using the [alias("myalias")] construct. This helps if you have a long variable name, but need a shorter alias for the commandline usage.
  • You can make parameters optional using the [parameter(Mandatory = $false)] construct.

e.g.

Param(
[parameter(Mandatory = $true)]
[alias("myalias1")]
[string]$mylongvariablename1,

[parameter(Mandatory = $false)]
[alias("myalias2")]
[string]$mylongvariablename2,
....

In this above example, you would call the script as script -myalias1 -myalias2 (myalias2 is of course, an optional param).

OTHER TIPS

I think I found the answer myself just now by poking around.

first of all you can to go the directory using

help [whatever_command] it will give you all the options

or sometime you have to use in my case (an exe file)

XsltTransform /?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top