Ric*_*olo 2 powershell scripting
我正在使用Powershell替换文本文件中的字符串.
为了在file.txt中替换String 1到String 2,我使用
(Get-Content file.ps1) | Foreach-Object { $_ -replace 'String 1', 'String 2'} | Set-Content file.txt
Run Code Online (Sandbox Code Playgroud)
它甚至适用于替换字符,如-和#
它需要用[字符替换字符串但它不起作用.我需要替换-state [VMstate] ::停止并使用-state停止,但它不起作用
(Get-Content TEMP_config.ps1) | Foreach-Object { $_ -replace '-state [VMstate]::stopped', '-state stopped'} | Set-Content TEMP_config.ps1
Run Code Online (Sandbox Code Playgroud)
我怎么能找到[ char?
replace 运算符使用正则表达式来匹配文本.搜索文字文本的一种方法是转义搜索文本:
$search = [regex]::escape('[MyText]')
(Get-Content file.ps1) | Foreach-Object { $_ -replace $search , 'String 2'} | Set-Content file.txt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
186 次 |
| 最近记录: |