简体   繁体   English

PowerShell读取属性文件

[英]PowerShell reading property file

I need read properties from config file. 我需要从配置文件读取属性。 I can do it with 我可以做到

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

But in this file I have param 但是在这个文件中我有参数

username=intranet\sysTestAcc

And when it's try to parse it I had error: Unrecognized escape sequence \\s. 当尝试解析它时,我遇到了错误:无法识别的转义序列\\ s。

How can I fix that with script? 如何使用脚本修复该问题? I can't change file before running script. 我无法在运行脚本之前更改文件。

Replace the \\ with \\\\ : \\替换为\\\\

# 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 '\\','\\')

You must escape the \\ with another one. 您必须将\\与另一个转义。 ie

username=intranet\\sysTestAcc

At that point ConvertFrom-StringData will be happy. 到那时,ConvertFrom-StringData将很高兴。

You might also use the "-Raw" parameter for Get-Content instead of pipeline into Out-String. 您可能还会对Get-Content使用“ -Raw”参数,而不是将其管道传递到Out-String中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM