Powershell 错误:方法调用...不包含名为“替换"的方法 [英] Powershell Error: Method invocation...doesn't contain a method named 'replace'
问题描述
我想使用 PowerShell 搜索和替换 xml 文件中的字符串.我试过这个:
I want to search and replace a string in an xml file using PowerShell. I tried this:
(gc d:\test.xml).replace('1234','xxxx')|sc d:\test.xml
这适用于我的 test.xml 文件.我的 test.xml 文件的内容是:
This works fine for my test.xml file. The content of my test.xml file is:
uehjduehduhfeuf xxxxxxxx 你好1234"
uehjduehduhfeuf xxxxxxxx hallo "1234"
但该文件仅用于测试.我想以这种方式编辑我的 tomcat 服务器中的 server.xml,如果我使用的命令与上面提到的完全相同,我会收到此错误消息:
But that file is only for testing. I want to edit a server.xml form my tomcat server in this way and if I'm using exactly the same command mentioned obove I get this error message:
方法调用失败,因为 [System.Object[]] 不包含名为replace"的方法.
Method invocation failed because [System.Object[]] doesn't contain a method named 'replace'.
有人知道这是什么问题吗?
Does anybody know what's the problem?
推荐答案
您还可以使用 [System.IO.File]::ReadAllText($filename)
从文件,包含所有换行符和其他符号.
You can also use [System.IO.File]::ReadAllText($filename)
to get a single string out of a file, with all newlines and other symbols.
[System.IO.File]::ReadAllText("d:\test.xml").replace('1234','xxxx')|sc d:\test.xml
这篇关于Powershell 错误:方法调用...不包含名为“替换"的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!