Предостережение : Используйте следующие решения только с входными файлами, которым вы доверяете , потому что можно вставлять произвольные команды в текст (предотвращение этого возможно, но требует больше работы)):
В сценарии PowerShell с именем, скажем, Expand-Text.ps1
:
# Define variables named for the placeholders in the input file,
# bound to the positional arguments passed.
$param1, $param2, $params = $args[0], $args[1], $args
# Read the whole text file as a single string.
$txt = Get-Content -Raw example.txt
# Replace '{...}' with '${...}' and then use
# PowerShell's regular string expansion (interpolation).
$ExecutionContext.InvokeCommand.ExpandString(($txt -replace '\{', '${'))
вызов .\Expand-Text.ps1 a b c
, а затем:
My name is a
I live in b
These are all the parameters passed to batch scripts a b c
В пакетном файле с именем, скажем, expandText.cmd
, с использованием интерфейса командной строки PowerShell:
@echo off
:: # \-escape double quotes so they're correctly passed through to PowerShell
set params=%*
set params=%params:"=\"%
:: "# Call PowerShell to perform the expansion.
powershell.exe -executionpolicy bypass -noprofile -c ^
"& { $txt = Get-Content -Raw example.txt; $param1, $param2, $params = $args[0], $args[1], $args; $ExecutionContext.InvokeCommand.ExpandString(($txt -replace '\{', '${')) }" ^
%params%