Reputation: 41
I am trying to add the text passed as an argument in powershell to config file. Below is the sample of the code. What I am expecting that below command will read the config file & search for parameter1 & when it find the parameter it will add the value (passed as an argument) after "="
(gc config.params) -replace "Parameter1 =", "$&` $1" | sc inifile.params
So the output supposed to be like: Parameter1 = hostname
when the following command will be executed: powershell.exe Untitled1.ps1 hostname
Please suggest.
Upvotes: 1
Views: 205
Reputation: 28993
$1
is not how arguments are passed to PowerShell scripts; they get an array $args
or you specify parameter names. And the array does not have the script path as the first element.
So, for your code:
(gc config.params) -replace "Parameter1 =", "$&` $($args[0])" | sc inifile.params
or
param($text)
(gc config.params) -replace "Parameter1 =", "$&` $text" | sc inifile.params
Upvotes: 1