-1

I have XML file with content below:

<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>

The tenants block contains an unknown count of an object in each of them path needs to be updated. The path has a UNC format which is also unknown. The task is to replace server name 1f7csgqde3l7.local (which is unknown) but it's the same in each path of the Tenants block with a variable $server, and URL 8s0n3lewbf7v.local with a variable $url.

What is the best approach to achieve this by using PowerShell?

Orest Gulman
  • 422
  • 1
  • 8
  • 25

2 Answers2

1

Probably not the best approach, but you can give it a try

Regex to get the UNC Path

[regex]$reg = "\\\\([a-z0-9_.$]+)\\"

Get-Content as XML

[xml]$xml = Get-Content -Path xml_file.xml

Loop through your structure, it simply replaces the matched UNC with your $url

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

And save it

$xml.Save("xml_file.xml")
newone
  • 111
  • 3
1

If I understand you correctly, you are looking for something like this:

$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);
}

That should result in

<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>
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45