Using Powershell to add attributes inside an XML

Viewed 49

I'm trying to make a script to add connexions to SAPWIN GUI by software distribution, but I have some difficulties with XML. I have this XML from initial installation of SAP : SAPUILandscape.xml in user profile.

<?xml version="1.0"?>
<Landscape updated="2022-09-19T12:11:12Z" version="1" generator="SAP GUI for Windows v7700.1.6.156">
    <Includes>
        <Include url="file:///C:/Users/toto/AppData/Roaming/SAP/Common/SAPUILandscapeGlobal.xml" index="0" description="SAP reserved"/>
    </Includes>
</Landscape>

Adding a connexion will add these lines in XML file.

  • Workspaces
    -- Workspace : uuid name
    --- Item : uuid serviceid

  • Messageservers
    -- Messageserver : uuid name host port

  • Services
    -- Service : type uuid name systemid msid server sncop dcpg

      <Landscape updated="2022-09-19T12:28:29Z" version="1" generator="SAP GUI for Windows v7700.1.6.156">
          <Workspaces>
              <Workspace uuid="89f722a0-f457-4dd5-8712-95b2a589d186" name="Local">
                  <Item uuid="282a2361-f701-48a2-87a9-2fb69c7e356d" serviceid="39876bd4-5f8a-4c9f-8fd0-5101a9928373"/>
              </Workspace>
          </Workspaces>
          <Messageservers>
              <Messageserver uuid="86c6edba-4679-43ac-b1c5-41b4779f583c" name="ABC" host="frsap01.domain.net" port="3621"/>
          </Messageservers>
          <Services>
              <Service type="SAPGUI" uuid="39876bd4-5f8a-4c9f-8fd0-5101a9928373" name="SAP ABC" systemid="ABC" msid="86c6edba-4679-43ac-b1c5-41b4779f583c" server="PUBLIC" sncop="-1" dcpg="2"/>
          </Services>
      </Landscape>
    

Adding another entry would change the file like this

<?xml version="1.0"?>
<Landscape updated="2022-09-19T12:37:51Z" version="1" generator="SAP GUI for Windows v7700.1.6.156">
    <Workspaces>
        <Workspace uuid="89f722a0-f457-4dd5-8712-95b2a589d186" name="Local">
            <Item uuid="282a2361-f701-48a2-87a9-2fb69c7e356d" serviceid="39876bd4-5f8a-4c9f-8fd0-5101a9928373"/>
            <Item uuid="7e924b57-df0a-4563-a29c-3e56fc0eaa1a" serviceid="f7267ba6-cc86-4999-86f1-6676bd32541f"/>
        </Workspace>
    </Workspaces>
    <Messageservers>
        <Messageserver uuid="86c6edba-4679-43ac-b1c5-41b4779f583c" name="ABC" host="frsap01.domain.net" port="3621"/>
        <Messageserver uuid="0fdbbf51-4c2e-4594-8572-8b308284f226" name="EFG" host="frsap02.domain.net" port="3655"/>
    </Messageservers>
    <Services>
        <Service type="SAPGUI" uuid="39876bd4-5f8a-4c9f-8fd0-5101a9928373" name="SAP ABC" systemid="ABC" msid="86c6edba-4679-43ac-b1c5-41b4779f583c" server="PUBLIC" sncop="-1" dcpg="2"/>
        <Service type="SAPGUI" uuid="f7267ba6-cc86-4999-86f1-6676bd32541f" name="SAP EFG" systemid="EFG" msid="0fdbbf51-4c2e-4594-8572-8b308284f226" server="PUBLIC" sncop="-1" dcpg="2"/>
    </Services>
    <Includes>
        <Include url="file:///C:/Users/WDAGUtilityAccount/AppData/Roaming/SAP/Common/SAPUILandscapeGlobal.xml" index="0" description="SAP Reserved"/>
    </Includes>
</Landscape>

I'll take it one step at a time. Here is what I've created so far

    # function to generate a GUID
    function UID {
    [System.GUID]::NewGuid().ToString()
    }
    # This is my date on the specific format
    $dateXML = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"
    # This is my SAP Version
    $VER = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplgmgr2.dll").FileVersion
    # This is the user connected executing the script
    $GetLoggeduser = $env:USERNAME
    
    # This is the path where I save my result
    $path = "C:\temp\SAPUILandscape.xml"
    
    # Initial XML (created on the first launch of SAP)
    $XMLinit = @'
    <?xml version="1.0"?>
    <Landscape updated="0{0}" version="1" generator="SAP GUI for Windows v{1}">
        <Includes>
            <Include url="file:///C:/Users/{2}/AppData/Roaming/SAP/Common/SAPUILandscapeGlobal.xml" index="0" description="SAP reserved"/>
        </Includes>
    </Landscape>
    '@ -f $dateXML,$ver,$GetLoggeduser
    
    # XML Format
    [xml]$xml = $XMLinit
    
    # Workspaces node creation
    $newNodeWorkspaces = $xml.CreateElement('Workspaces')
    $xml.Landscape.AppendChild($newNodeWorkspaces)
    
    
    # Workspace node creation
    $newNodeWorkspace = $xml.CreateElement('Workspace')
    $uuidWorkspace = UID
    $newNodeWorkspace.SetAttribute("uuid",$uuidWorkspace)
    $newNodeWorkspace.SetAttribute("name","Local")
    $xml.Landscape.AppendChild($newNodeWorkspace)
    
    
    # Saving XML
    $xml.Save($path)

And this is my result

<?xml version="1.0"?>
<Landscape updated="02022-09-20T13:55:17Z" version="1" generator="SAP GUI for Windows v7700.1.6.156">
  <Includes>
    <Include url="file:///C:/Users/toto/AppData/Roaming/SAP/Common/SAPUILandscapeGlobal.xml" index="0" description="SAP reserved" />
  </Includes>
  <Workspaces />
  <Workspace uuid="2410d872-5264-475a-a19f-fb8d184d5bfd" name="Local" />
</Landscape>

This is what I wanted

<?xml version="1.0"?>
<Landscape updated="02022-09-20T13:55:17Z" version="1" generator="SAP GUI for Windows v7700.1.6.156">
    <Includes>
        <Include url="file:///C:/Users/toto/AppData/Roaming/SAP/Common/SAPUILandscapeGlobal.xml" index="0" description="SAP reserved"/>
    </Includes>
<Workspaces>
    <Workspace uuid="2410d872-5264-475a-a19f-fb8d184d5bfd" name="Local">
    </Workspace>
</Workspaces>
</Landscape>

Better, but. The result should be like this

<Workspaces>
  <Workspace uuid="89f722a0-f457-4dd5-8712-95b2a589d186" name="Local">
  </Workspace>
</Workspaces>

name="Local"> **not** name="Local"**/**>
<Workspace uuid="89f722a0-f457-4dd5-8712-95b2a589d186" name="Local">
</Workspace>

**not**
<Workspace uuid="8b2af325-9c34-4a2f-8ba6-82e3d24053fa" name="Local" />
1 Answers

You create the new nodes allright, but append the inner Workspace node to the wrong parent node.

Try

# Workspaces node creation
$newNodeWorkspaces = $xml.CreateElement('Workspaces')
# Workspace node creation
$newNodeWorkspace = $xml.CreateElement('Workspace')
$uuidWorkspace = [System.GUID]::NewGuid().Guid
$newNodeWorkspace.SetAttribute("uuid",$uuidWorkspace)
$newNodeWorkspace.SetAttribute("name","Local")

# append the Workspace node to the new Workspaces node
[void]$newNodeWorkspaces.AppendChild($newNodeWorkspace)

# then append that to the $xml.Landscape node
[void]$xml.Landscape.AppendChild($newNodeWorkspaces)

# Saving XML
$xml.Save($path)

Result:

<?xml version="1.0"?>
<Landscape updated="02022-09-20T14:31:24Z" version="1" generator="SAP GUI for Windows v1.2.3.4">
  <Includes>
    <Include url="file:///C:/Users/Theo/AppData/Roaming/SAP/Common/SAPUILandscapeGlobal.xml" index="0" description="SAP reserved" />
  </Includes>
  <Workspaces>
    <Workspace uuid="b4f5fb29-2d1e-402f-afe1-f10e6a753722" name="Local" />
  </Workspaces>
</Landscape>
Related