Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Powershell中使用参数在文件中添加文本_Powershell - Fatal编程技术网

如何在Powershell中使用参数在文件中添加文本

如何在Powershell中使用参数在文件中添加文本,powershell,Powershell,我正在尝试将powershell中作为参数传递的文本添加到配置文件中。下面是代码示例。我期望下面的命令将读取配置文件&搜索参数1&当它找到参数时,它将在“=”之后添加值(作为参数传递) 所以输出应该是这样的: 参数1=主机名 执行以下命令时: powershell.exe Untitled1.ps1主机名 请建议 $1不是参数传递给PowerShell脚本的方式;它们获得一个数组$args,或者您指定参数名。并且数组没有脚本路径作为第一个元素 因此,对于您的代码: (gc config.para

我正在尝试将powershell中作为参数传递的文本添加到配置文件中。下面是代码示例。我期望下面的命令将读取配置文件&搜索参数1&当它找到参数时,它将在“=”之后添加值(作为参数传递)

所以输出应该是这样的: 参数1=主机名

执行以下命令时: powershell.exe Untitled1.ps1主机名


请建议

$1
不是参数传递给PowerShell脚本的方式;它们获得一个数组
$args
,或者您指定参数名。并且数组没有脚本路径作为第一个元素

因此,对于您的代码:

(gc config.params) -replace "Parameter1 =", "$&` $($args[0])" | sc inifile.params


哇,
$&
在正则表达式替换字符串中,我以前怎么没见过这种情况!我执行了第一个命令,它给了我以下输出:Parameter1=[0]我在脚本中放入
“Parameter1=“-replace”Parameter1=“,“$&$($args[0])”
,然后运行
\test.ps1 hostname
并获取
Parameter1=hostname
。。你有什么确切的密码?您是否确实在变量周围放置了
$()
?您是在shell提示符下运行它的,其中
$args
什么都不是,还是将其放入脚本中?我的代码是:(gc inifile.params)-替换为“NW_CI_Instance.ciVirtualHostname=“,”$&$($args[0])”;sc inifile.params&我使用powershell.exe untilled1.ps1 test命令运行它
(gc config.params) -replace "Parameter1 =", "$&` $($args[0])" | sc inifile.params
param($text)

(gc config.params) -replace "Parameter1 =", "$&` $text" | sc inifile.params