使用PowerShell替换XML文件中所有对象的值

使用PowerShell替换XML文件中所有对象的值,xml,powershell,Xml,Powershell,我有以下内容的XML文件: <Tenants> <Tenant name="tenant_1" url="8s0n3lewbf7v.local" site="98074" path="\\1f7csgqde3l7.local\share\shared\98074" /> <Tenant name="tenant_2" url="8s0n3le

我有以下内容的XML文件:

<Tenants>
    <Tenant name="tenant_1" url="8s0n3lewbf7v.local" site="98074" path="\\1f7csgqde3l7.local\share\shared\98074" />
    <Tenant name="tenant_2" url="8s0n3lewbf7v.local" site="62761" path="\\1f7csgqde3l7.local\share\shared\62761" />
    <Tenant name="tenant_3" url="8s0n3lewbf7v.local" site="24387" path="\\1f7csgqde3l7.local\share\shared\24387" />
    <Tenant name="tenant_4" url="8s0n3lewbf7v.local" site="85670" path="\\1f7csgqde3l7.local\share\shared\85670" />
    <Tenant name="tenant_5" url="8s0n3lewbf7v.local" site="29117" path="\\1f7csgqde3l7.local\share\shared\29117" />
</Tenants>
租户块包含每个租户中对象的未知计数。路径需要更新。 路径的UNC格式也是未知的。 任务是用变量$server替换服务器名1f7csgqde3l7.local,该名称未知,但在租户块的每个路径中都相同,URL 8s0n3lewbf7v.local替换为变量$URL


使用PowerShell实现这一目标的最佳方法是什么?

可能不是最佳方法,但您可以尝试一下

Regex以获取UNC路径

[regex]$reg = "\\\\([a-z0-9_.$]+)\\"
以XML形式获取内容

[xml]$xml = Get-Content -Path xml_file.xml
在结构中循环,它只是用$url替换匹配的UNC

Foreach($x in $xml.Tenants.Tenant){
   $x.path = $x.path.Replace($reg.Match($x.path).Value,"\\$url\")
}
并保存它

$xml.Save("xml_file.xml")

如果我理解正确,您正在寻找以下内容:

$xml = [xml]'<Tenants>
    <Tenant name="tenant_1" url="8s0n3lewbf7v.local" path="\\1f7csgqde3l7.local\share\shared\98074_prod" />
    <Tenant name="tenant_2" url="8s0n3lewbf7v.local" path="\\1f7csgqde3l7.local\share\shared\62761_prod" />
    <Tenant name="tenant_3" url="8s0n3lewbf7v.local" path="\\1f7csgqde3l7.local\share\shared\24387_prod" />
    <Tenant name="tenant_4" url="8s0n3lewbf7v.local" path="\\1f7csgqde3l7.local\share\shared\85670_prod" />
    <Tenant name="tenant_5" url="8s0n3lewbf7v.local" path="\\1f7csgqde3l7.local\share\shared\29117_prod" />
</Tenants>
'
$new_server = "abcd123cde56"
$new_url="123456789asd.local"

$nodes = $xml.SelectNodes(".//Tenants//Tenant");
foreach($node in $nodes) {
    $target_path =  $node.GetAttribute("path")
    $delim = ".lo"
    $name = $target_path -split $delim
    $newname = -join("\\",$new_server,$delim ,$name[1]);    
    $node.SetAttribute("path", $newname);
    $node.SetAttribute("url", $new_url);
}
这将导致

<Tenants>
  <Tenant name="tenant_1" url="123456789asd.local" path="\\abcd123cde56.local\share\shared\98074_prod" />
  <Tenant name="tenant_2" url="123456789asd.local" path="\\abcd123cde56.local\share\shared\62761_prod" />
  <Tenant name="tenant_3" url="123456789asd.local" path="\\abcd123cde56.local\share\shared\24387_prod" />
  <Tenant name="tenant_4" url="123456789asd.local" path="\\abcd123cde56.local\share\shared\85670_prod" />
  <Tenant name="tenant_5" url="123456789asd.local" path="\\abcd123cde56.local\share\shared\29117_prod" />
</Tenants>

这有用吗?是的,这就是我一直在寻找的解决方案,谢谢[xml]$config=Get Content$file Foreach$I in$config.Tenant.Tenants.childnodes{$I.path=$I.path.Replace$reg.Match$I.path.Value,\\\$server\$I.url=$url}