PowerShell 正在读取 属性 个文件

PowerShell reading property file

我需要从配置文件中读取属性。 我可以用

ConvertFrom-StringData(Get-Content test.conf | out-String)

但是在这个文件中我有参数

username=intranet\sysTestAcc

当它试图解析它时我遇到了错误: 无法识别的转义序列 \s.

我怎样才能用脚本解决这个问题?我无法在 运行 脚本之前更改文件。

你必须用另一个转义\。即

username=intranet\sysTestAcc

到那时 ConvertFrom-StringData 会很高兴。

您也可以对 Get-Content 使用“-Raw”参数,而不是通过管道传输到 Out-String。

\替换为\:

# The first '\' is a regex with a '\' escape to create a literal '\'
# The second '\' is a literal replacement (non-regex) that inserts two '\'
ConvertFrom-StringData ((Get-Content test.conf -Raw) -replace '\','\')