![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | How to run PS script with parameters from cmd batch file? 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 $. Such a simple task becomes very tricky because of the current (I hope) PowerShell command line syntax. The syntax: powershell.exe ... [-Command] string [remainingArgs] is not enough for using in batch files. It is also pain not to forget to escape all/some special symbols manually in cmd command line. Files names do contain symbols `’$#{}()[] and etc. I would like to have alternative like powershell.exe ... -Script script.ps1 [scriptArgs] where scriptArgs are passed to script.ps1 as they are. WSH, Perl, ... can do that. Well, I solved the problem with a Perl script which escapes PS special symbols and recalls powershell.exe. Thus, now in command files I do Run-PS.pl - .\MyScript.ps1 “%1” “%2” “%MyEnvVar%” The idea of mentioned Perl script: # Run-PS.pl for(my $e = 0; $e <= $#ARGV; ++$e) { # all before ‘-‘ is unchanged next if $ARGV[$e] ne '-'; # ‘-‘ is converted into ‘&’ $ARGV[$e] = '"&"'; # all after ‘-‘ is escaped for(++$e; $e <= $#ARGV; ++$e) { $ARGV[$e] =~ s/([ `'"@#(){}[]\$])/`$1/g; } } system 'powershell.exe', @ARGV; I do not really like necessity of this workaround. Any other ideas? -- Thanks, Roman |
My System Specs![]() |
| | #2 (permalink) |
| | RE: How to run PS script with parameters from cmd batch file? Sorry for a mistake in my Perl script. The correction is here: # Run-PS.pl for(my $e = 0; $e <= $#ARGV; ++$e) { next if $ARGV[$e] ne '-'; $ARGV[$e] = '"&"'; for(++$e; $e <= $#ARGV; ++$e) { $ARGV[$e] =~ s/([ `'"@#(){}\[\]\$])/`$1/g; } } system 'powershell.exe', @ARGV; |
My System Specs![]() |
| | #3 (permalink) |
| | RE: How to run PS script with parameters from cmd batch file? Well, I did I quick test. I'm not sure wheather this helps you with your problem regarding batch variables (I'm not an expert in batch scripting), but this is what I noticed: # the content of my test script (which is in $pshome directory) >Get-Content test.ps1 $foo = $args $foo | Get-Member Write-Host $foo # testing from cmd.exe (your path to powershell.exe might differ) C:\>"C:\Programme\Windows PowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -Noninteractive -Command test.ps1 "foobar,123,$pshome (Get-Date)" Noticed that the output of the Write-Host cmdlet in the test.ps1 script is written to the console between the member information of System.Object[] and System.DateTime. PowerShell formatting still puzzles me. -- greetings dreeschkind "Roman Kuzmin" wrote: > 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 $. > > Such a simple task becomes very tricky because of the current (I hope) > PowerShell command line syntax. > > The syntax: > > powershell.exe ... [-Command] string [remainingArgs] > > is not enough for using in batch files. It is also pain not to forget to > escape all/some special symbols manually in cmd command line. Files names do > contain symbols `’$#{}()[] and etc. > > I would like to have alternative like > > powershell.exe ... -Script script.ps1 [scriptArgs] > > where scriptArgs are passed to script.ps1 as they are. WSH, Perl, ... can do > that. > > Well, I solved the problem with a Perl script which escapes PS special > symbols and recalls powershell.exe. Thus, now in command files I do > > Run-PS.pl - .\MyScript.ps1 “%1” “%2” “%MyEnvVar%” > > The idea of mentioned Perl script: > > # Run-PS.pl > for(my $e = 0; $e <= $#ARGV; ++$e) > { > # all before ‘-‘ is unchanged > next if $ARGV[$e] ne '-'; > # ‘-‘ is converted into ‘&’ > $ARGV[$e] = '"&"'; > # all after ‘-‘ is escaped > for(++$e; $e <= $#ARGV; ++$e) > { > $ARGV[$e] =~ s/([ `'"@#(){}[]\$])/`$1/g; > } > } > system 'powershell.exe', @ARGV; > > I do not really like necessity of this workaround. Any other ideas? > > -- > Thanks, > Roman |
My System Specs![]() |
| | #4 (permalink) |
| | RE: How to run PS script with parameters from cmd batch file? Yes, it is funny. Meanwhile I think your issue should be better posted as a separate thread, I do not see any relations with this thread. -- Thanks, Roman "dreeschkind" wrote: > Well, I did I quick test. I'm not sure wheather this helps you with your > problem regarding batch variables (I'm not an expert in batch scripting), but > this is what I noticed: > > # the content of my test script (which is in $pshome directory) > >Get-Content test.ps1 > $foo = $args > $foo | Get-Member > Write-Host $foo > > # testing from cmd.exe (your path to powershell.exe might differ) > C:\>"C:\Programme\Windows PowerShell\v1.0\powershell.exe" -NoLogo -NoProfile > -Noninteractive -Command test.ps1 "foobar,123,$pshome (Get-Date)" > > Noticed that the output of the Write-Host cmdlet in the test.ps1 script is > written to the console between the member information of System.Object[] and > System.DateTime. > > PowerShell formatting still puzzles me. > > -- > greetings > dreeschkind > > "Roman Kuzmin" wrote: > > > 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 $. > > > > Such a simple task becomes very tricky because of the current (I hope) > > PowerShell command line syntax. > > > > The syntax: > > > > powershell.exe ... [-Command] string [remainingArgs] > > > > is not enough for using in batch files. It is also pain not to forget to > > escape all/some special symbols manually in cmd command line. Files names do > > contain symbols `’$#{}()[] and etc. > > > > I would like to have alternative like > > > > powershell.exe ... -Script script.ps1 [scriptArgs] > > > > where scriptArgs are passed to script.ps1 as they are. WSH, Perl, ... can do > > that. > > > > Well, I solved the problem with a Perl script which escapes PS special > > symbols and recalls powershell.exe. Thus, now in command files I do > > > > Run-PS.pl - .\MyScript.ps1 “%1” “%2” “%MyEnvVar%” > > > > The idea of mentioned Perl script: > > > > # Run-PS.pl > > for(my $e = 0; $e <= $#ARGV; ++$e) > > { > > # all before ‘-‘ is unchanged > > next if $ARGV[$e] ne '-'; > > # ‘-‘ is converted into ‘&’ > > $ARGV[$e] = '"&"'; > > # all after ‘-‘ is escaped > > for(++$e; $e <= $#ARGV; ++$e) > > { > > $ARGV[$e] =~ s/([ `'"@#(){}[]\$])/`$1/g; > > } > > } > > system 'powershell.exe', @ARGV; > > > > I do not really like necessity of this workaround. Any other ideas? > > > > -- > > Thanks, > > Roman |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How to run PS script with parameters from cmd batch file? Hi Roman; Our recent builds have introduced a feature where we do not parse single-quoted strings at all. You'll see this in the next drop: C:\temp>powershell -command .\ParamTest.ps1 't`e$''st' Input is t`e$'st Note that the one character you DO have to treat specially is the single quote, because it ends a single-quoted string. Two single quotes are converted into one. Your suggestion for the -Script parameter is a good one, and something that is on our radar for a future release. -- Lee Holmes [MSFT] Windows PowerShell Development Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message news:9A20933B-35E5-4AE6-A329-92C58834161D@microsoft.com... > 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 $. > > Such a simple task becomes very tricky because of the current (I hope) > PowerShell command line syntax. > > The syntax: > > powershell.exe ... [-Command] string [remainingArgs] > > is not enough for using in batch files. It is also pain not to forget to > escape all/some special symbols manually in cmd command line. Files names > do > contain symbols `'$#{}()[] and etc. > > I would like to have alternative like > > powershell.exe ... -Script script.ps1 [scriptArgs] > > where scriptArgs are passed to script.ps1 as they are. WSH, Perl, ... can > do > that. > > Well, I solved the problem with a Perl script which escapes PS special > symbols and recalls powershell.exe. Thus, now in command files I do > > Run-PS.pl - .\MyScript.ps1 "%1" "%2" "%MyEnvVar%" > > The idea of mentioned Perl script: > > # Run-PS.pl > for(my $e = 0; $e <= $#ARGV; ++$e) > { > # all before '-' is unchanged > next if $ARGV[$e] ne '-'; > # '-' is converted into '&' > $ARGV[$e] = '"&"'; > # all after '-' is escaped > for(++$e; $e <= $#ARGV; ++$e) > { > $ARGV[$e] =~ s/([ `'"@#(){}[]\$])/`$1/g; > } > } > system 'powershell.exe', @ARGV; > > I do not really like necessity of this workaround. Any other ideas? > > -- > Thanks, > Roman |
My System Specs![]() |
| | #6 (permalink) |
| | Re: How to run PS script with parameters from cmd batch file? Hi Lee, > Our recent builds have introduced a feature where we do not parse > single-quoted strings at all. I vote for this, it makes things a bit easier, so it is good. > Note that the one character you DO have to treat specially is the single > quote, because it ends a single-quoted string. Two single quotes are > converted into one. Of course, it is inevitable. And this still makes difficulties for easy use of PowerShell in cmd batch files for calling PS scripts with parameters. > Your suggestion for the -Script parameter is a good one, and something that > is on our radar for a future release. I do think it is just necessarily for PowerShell integration into the real world. Not only cmd batch files suffer now. There are many products that provide external tools mechanism via command lines. Visual Studio, for example. Suppose, I want to make a Visual Studio external tool like PowerShell.exe … -Command …\MyScript.ps1 “’$(ItemPath)’” The same problem is here like in batch: we can not escape something or use double ‘’ here. If $(ItemPath) contains ‘ - it will fail. -- Thanks, Roman |
My System Specs![]() |
![]() |
| 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 a batch file | PowerShell | |||