Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to run PS script with parameters from a batch file

Reply
 
Old 09-09-2006   #1 (permalink)
=?Utf-8?B?Um9tYW4gS3V6bWlu?=


 
 

How to run PS script with parameters from a batch file

How to run PS script with parameters from a batch file

Few weeks ago I posted the same question and an easy solution was not found.
I have just discovered a very simple way to solve the problem.

PROBLEM

Quote:
I’d like to run a PS script with parameters from a cmd batch file. These
parameters are actually batch variables like %1, %2, %MyEnvVar% and etc. The
commands:
powershell.exe -command .\MyScript.ps1 '%1' '%2' '%MyEnvVar%'
powershell.exe -command .\MyScript.ps1 "%1" "%2" "%MyEnvVar%"
powershell.exe -command .\MyScript.ps1 "'%1'" "'%2'" "'%MyEnvVar%'"
are not good at all if parameter values contains for example `, ' or $.
SOLUTION

The trick is to pass %1 and %2 values via environment variables. Note that
nothing should be quoted or escaped at all:

[batch]
set arg1=%~1
set arg2=%~2
PowerShell -command .\MyScript.ps1 $env:arg1 $env:arg2 $env:MyEnvVar
[/batch]

CAVEATS

For existing %MyEnvVar% I do not see any caveats. If %1, %2 are regular data
like paths or wildcards - I do not see problems too. Please, correct me if I
miss something.

This way will not work only if %1, %2 contain symbols special for cmd: for
example >, &, |. Such data still require another solution. But they look
problematic for batch files themselves.

--
Thanks,
Roman

My System SpecsSystem Spec
Old 09-09-2006   #2 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: How to run PS script with parameters from a batch file

You can handle the special cases you note at bottom by escaping those
special characters with a preceding "^". Example:

C:\temp>echo ^|^>^^^&^<
|>^&<

I've already done something similar to this, but it was about creating a
shortcut, _not_ invoking a powershell script from cmd.exe. The problem you
show is actually much more complicated.

"Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message
news:F749EAFE-2F8C-496A-8D01-B81E9257F0A9@microsoft.com...
> How to run PS script with parameters from a batch file
>
> Few weeks ago I posted the same question and an easy solution was not
> found.
> I have just discovered a very simple way to solve the problem.
>
> PROBLEM
>
>
Quote:
> I'd like to run a PS script with parameters from a cmd batch file. These
> parameters are actually batch variables like %1, %2, %MyEnvVar% and etc.
> The
> commands:
> powershell.exe -command .\MyScript.ps1 '%1' '%2' '%MyEnvVar%'
> powershell.exe -command .\MyScript.ps1 "%1" "%2" "%MyEnvVar%"
> powershell.exe -command .\MyScript.ps1 "'%1'" "'%2'" "'%MyEnvVar%'"
> are not good at all if parameter values contains for example `, ' or $.
>
>
> SOLUTION
>
> The trick is to pass %1 and %2 values via environment variables. Note that
> nothing should be quoted or escaped at all:
>
> [batch]
> set arg1=%~1
> set arg2=%~2
> PowerShell -command .\MyScript.ps1 $env:arg1 $env:arg2 $env:MyEnvVar
> [/batch]
>
> CAVEATS
>
> For existing %MyEnvVar% I do not see any caveats. If %1, %2 are regular
> data
> like paths or wildcards - I do not see problems too. Please, correct me if
> I
> miss something.
>
> This way will not work only if %1, %2 contain symbols special for cmd: for
> example >, &, |. Such data still require another solution. But they look
> problematic for batch files themselves.
>
> --
> Thanks,
> Roman



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
batch file / script to replace information VB Script
How can I play a Wav file from batch/cmd script? Vista performance & maintenance
Help with a Batch file script (FTP) Vista General
Call powershell script via Batch-File PowerShell
How to run PS script with parameters from cmd batch file? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46