I have a xml-file with a structure as following and I would like to edit this file from the command line.
我有一个结构如下的xml文件,我想从命令行编辑这个文件。
<server>
<service>
<Connector port="8080" password="password1234"/>
</service>
</server>
I would like to change the password or the port-number. Does cmd provide this option or do I need an extra tool? I know powershell can do it but that's not the best solution for me. (Besides I didn't get it run with powershell :( ). It would be also ok to search for "password1234" and replace it, because there is a default password in my file which is always the same and this must be replaced.
我想更改密码或端口号。 cmd是否提供此选项,还是需要额外的工具?我知道powershell可以做到,但这对我来说不是最好的解决方案。 (除了我没有运行powershell :()。搜索“password1234”并替换它也是可以的,因为我的文件中有一个默认密码,它始终是相同的,必须更换。
1 个解决方案
#1
2
To demonstrate one method I use let first create the xml file in your example:
为了演示我使用的一个方法,首先在您的示例中创建xml文件:
Define a Variable for the XML Filename
为XML文件名定义变量
$xmlFile = "C:\temp\myconfig.xml"
Define an XML String to save to the file
定义要保存到文件的XML String
$xmlFromString = [xml]@"
<server>
<service>
<Connector port="8080" password="password1234"/>
</service>
</server>
"@
Save the xml contents to the file
将xml内容保存到文件中
$xmlFromString.Save($xmlFile)
Resulting file content
产生的文件内容
Get-Content -Path $xmlFile
<server> <service> <Connector port="8080" password="password1234" /> </service> </server>
Here is the PowerShell code to change the values Get XML content from file
以下是更改值从文件中获取XML内容的PowerShell代码
$xml = [xml](Get-Content -Path $xmlFile)
Finds the Element / Node and change the attribute values
查找元素/节点并更改属性值
$node = $xml.selectSingleNode('//server/service/Connector')
$node.port = "9090"
$node.password = "MyNewPassord4321"
Save the XML Contents Back out
保存XML内容退出
$xml.Save($xmlFile)
Results
Get-Content -Path $xmlFile
<server> <service> <Connector port="9090" password="MyNewPassord4321" /> </service> </server>
Save the commands to a PowerShell ps1 file and execute/run it via PowerShell.
将命令保存到PowerShell ps1文件并通过PowerShell执行/运行它。
We'll need additional details on what exactly your trying to accomplish such as:
我们需要有关您尝试完成的具体内容的其他详细信息,例如:
- What rights will the user / account running the script has?
- Where will the script be running from? Local PC or server?
- One or multiple servers/work stations?
- Executed via Windows Scheduler Task?
运行脚本的用户/帐户有哪些权限?
脚本将从何处运行?本地PC还是服务器?
一个或多个服务器/工作站?
通过Windows Scheduler Task执行?
Hope that was helpful. - *s
希望这很有帮助。 - 布鲁克斯
#1
2
To demonstrate one method I use let first create the xml file in your example:
为了演示我使用的一个方法,首先在您的示例中创建xml文件:
Define a Variable for the XML Filename
为XML文件名定义变量
$xmlFile = "C:\temp\myconfig.xml"
Define an XML String to save to the file
定义要保存到文件的XML String
$xmlFromString = [xml]@"
<server>
<service>
<Connector port="8080" password="password1234"/>
</service>
</server>
"@
Save the xml contents to the file
将xml内容保存到文件中
$xmlFromString.Save($xmlFile)
Resulting file content
产生的文件内容
Get-Content -Path $xmlFile
<server> <service> <Connector port="8080" password="password1234" /> </service> </server>
Here is the PowerShell code to change the values Get XML content from file
以下是更改值从文件中获取XML内容的PowerShell代码
$xml = [xml](Get-Content -Path $xmlFile)
Finds the Element / Node and change the attribute values
查找元素/节点并更改属性值
$node = $xml.selectSingleNode('//server/service/Connector')
$node.port = "9090"
$node.password = "MyNewPassord4321"
Save the XML Contents Back out
保存XML内容退出
$xml.Save($xmlFile)
Results
Get-Content -Path $xmlFile
<server> <service> <Connector port="9090" password="MyNewPassord4321" /> </service> </server>
Save the commands to a PowerShell ps1 file and execute/run it via PowerShell.
将命令保存到PowerShell ps1文件并通过PowerShell执行/运行它。
We'll need additional details on what exactly your trying to accomplish such as:
我们需要有关您尝试完成的具体内容的其他详细信息,例如:
- What rights will the user / account running the script has?
- Where will the script be running from? Local PC or server?
- One or multiple servers/work stations?
- Executed via Windows Scheduler Task?
运行脚本的用户/帐户有哪些权限?
脚本将从何处运行?本地PC还是服务器?
一个或多个服务器/工作站?
通过Windows Scheduler Task执行?
Hope that was helpful. - *s
希望这很有帮助。 - 布鲁克斯