Passing argument from batch script to PowerShell script fails

Tag: powershell , batch-file , command , argument-passing Author: accpa586 Date: 2013-06-27

I am trying to find out a solution by passing the REPLACE command through the batch file as the "ReplaceTagsOnConfigFiles.ps1" will work when the file is executed with REPLACE option or otherwise, it will ask for the [REPLACE | ROLLBACK]

Below is the batch file:

echo "Changing the execution policy"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command {Set-ExecutionPolicy} > C:\Temp\a.out

echo "Updating the Server details:"

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "D:\test\Bluebird_1.2.0_RTM\ReplaceTagsOnConfigFiles.ps1;REPLACE" > C:\Temp\b.out

When the above batch file is executed, it pops up the message as shown below.

Available commands:
* Replace all strings to get the new configuration from every .config and .x aml file.
* Remove the current configuration and retrieving the old ones (the replace must be executed before).

ReplaceTagsOnConfigFiles.ps1 [REPLACE | ROLLBACK]

REPLACE Replaces all strings in every .config and .xaml file to build the new configuration.
ROLLBACK Retrieving the old configuration from backups. NOTE: this option is useful just if you used the 'replace' one before.

No files replaced

Kindly help me in this regard.

Other Answer1

Change this command:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "D:\test\Bluebird_1.2.0_RTM\ReplaceTagsOnConfigFiles.ps1;REPLACE" > C:\Temp\b.out

into this:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "D:\test\Bluebird_1.2.0_RTM\ReplaceTagsOnConfigFiles.ps1" REPLACE > C:\Temp\b.out

Is the first command supposed to change the execution policy? If so, you need to specify an actual policy. Set-ExecutionPolicy without any arguments won't work. Besides, it'd be simpler to just add -ExecutionPolicy Bypass to the second command:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "D:\test\Bluebird_1.2.0_RTM\ReplaceTagsOnConfigFiles.ps1" REPLACE > C:\Temp\b.out


When I changed the above and executed it as batch file, I couldn't find any log file generated and when trying to drag and drop the batch file in the command prompt, it throws the info as follows:WARNING There are already some back files. Do you want to continue with the process? [Y] Yes [N] No [?] Help (default is "N"): Y
I don't think your execution policy handling is working as you expect. See updated answer. Can't say anything about the warning without seeing your PowerShell code.
I am unable to paste the code here. Is there any other solution to this one.
No. AFAICS the message is generated by your script, so you need to either show the code or debug it yourself. We're not playing guessing games here.
I do understand that you are helping me. How can I paste the code here when only a few characters could be entered here in the comment box as my code has more than this. I am sharing the file through the link

Other Answer2

The error you're getting is not from PowerShell directly. Most of it (all except the last line) is from ReplaceTagsOnConfigFiles.ps1. The problem is that this script expects either REPLACE or ROLLBACK as an argument (as indicated by the error message), but you're passing it no arguments. The semicolon is a command separator (like using an ampersand in cmd), so you're telling PowerShell to first execute D:\test\Bluebird_1.2.0_RTM\ReplaceTagsOnConfigFiles.ps1 by itself with no arguments. Then you're executing the REPLACE command, again by itself with no arguments. That's what gives you the "No files replaced" error.

All you need to do is replace the semicolon with a space, to make REPLACE an argument to ReplaceTagsOnConfigFiles.ps1 rather than a new command.

BTW, speaking of ampersands, in PowerShell they're used to execute non-native commands (including .ps1 script files). The way you have it here works because there are no spaces in the path, but you'll run into problems if you ever need to quote the path (Powershell will echo it as a string rather than executing it). You should get into the habit of doing this:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'D:\test\Bluebird_1.2.0_RTM\ReplaceTagsOnConfigFiles.ps1' REPLACE" > C:\Temp\b.out


Since you're new to developark, note that if an answer resolves your problem, you should click the check mark to the left of that answer in order to mark it as accepted.