简体   繁体   English

更改存档上的指定值 - PS

[英]Change a specified value on archive - PS

i have this following configurations in archive, to be specific on line 15 i have to change string "srv-01.address" file name is UpdateClient.config , how can i change this on powershell?我在存档中有以下配置,具体在第 15 行我必须更改字符串"srv-01.address"文件名是UpdateClient.config ,我如何在 powershell 上更改它?

<ProviderServers>
    <ProviderServer URL="srv-01.address" IsDefault="False" Port="8092" SecureConnection="False" BindingType="BasicHttpBinding" />
  </ProviderServers>

If that is an xml file import it and set the attribute like:如果那是一个 xml 文件导入它并设置如下属性:

$xml = [xml](gc "FILEPATH")
$xml.ProviderServers.ProviderServer.setattribute("URL","UpdateClient.config")

One way would be to use XPath (which is Case-Sensitive)一种方法是使用 XPath(区分大小写)

$search  = "srv-01.address"
$replace = "srv-02.address"

[xml]$xml = Get-Content -Path 'Path\To\UpdateClient.config' -Raw
$xml.SelectNodes("//ProviderServer[@URL='$search']") | ForEach-Object { $_.SetAttribute('URL', $replace) }
$xml.Save('Path\To\UpdateClient.config')

You can also use the Case-Insensitive 'dot' method, but for that to realy work, you didn't show enough of the.config xml file.您也可以使用不区分大小写的“点”方法,但要真正发挥作用,您没有显示足够的 .config xml 文件。 What it boils down to is that you should follow the structure all the way from the root element.它归结为您应该从根元素开始一直遵循结构。

$search  = "srv-01.address"
$replace = "srv-02.address"

[xml]$xml = Get-Content -Path 'Path\To\UpdateClient.config' -Raw
$xml.providerservers.providerserver | Where-Object { $_.Url -eq $search } | ForEach-Object { $_.SetAttribute('URL', $replace) }
$xml.Save('Path\To\UpdateClient.config')

Both methods will update the file to contain两种方法都会更新文件以包含

<ProviderServers>
  <ProviderServer URL="srv-02.address" IsDefault="False" Port="8092" SecureConnection="False" BindingType="BasicHttpBinding" />
</ProviderServers>

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

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