user2578953
user2578953

Reputation: 1

Passing argument from batch script to PowerShell script fails

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.

Upvotes: 0

Views: 982

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

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

Upvotes: 1

Adi Inbar
Adi Inbar

Reputation: 12323

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

Upvotes: 0

Related Questions