Donnerstag, 17. Januar 2013

Call Powershell Scripts with Parameters

often you need to pass parameters when you call your powershell scripts. This is very easy and quickly done.

By passing arguments to your script you create a kind of array of arguments, correct me if I am wrong.

But to use them, you treat them like an array.

To assign a passed argument to a new variable use this:

$var1 = $args[0]
$var2 = $args[1]

very easy, isn't it??

I prefer to call my scripts not directly, because I dont like to write the whole calling command.
The way i prefer is to use command files.

The only thing you need to write into your file is:

powershell.exe -file "<path>" arg1 arg2 arg3 ...

If you want to log all errors type 2> behind your command:

powershell.exe -file "<path>" arg1 arg2 arg3  2> <path>

There could be one hard failure which could make you desperate.
Your script works fine, your call is also fine, but your paramters are not passed to your script. WHY?

It costs me a lot of time, but at the end the failure is hardly hidden :)

The way I name my command files is call_myscript.cmd. This would work.

If you name your command file call_mysscript.ps1.cmd this would also call your script. But it would not pass your parameters. Don't ask me why.

Hope this helps you.

Keine Kommentare:

Kommentar veröffentlichen