Deano
Deano

Reputation: 155

Powershell - Add New Data to XML

I have a part of the script that calls an API for the attributes of the host. If API fails, it checks if there is text file containing the attributes (from a previous run). If the file does not exist, I need to add UNKNOWN to the attributes node. See the expected out come in the XML below. The XML will start of with no attributes as the script is meant to populate this info.

  <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <netprobe compatibility="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.itrsgroup.com/GA2011.2-110303/netprobe.xsd">
- <selfAnnounce>
  <enabled>true</enabled> 
  <retryInterval>60</retryInterval> 
  <requireReverseConnection>false</requireReverseConnection> 
- <probeName>
  <hostname /> 
  <data>_</data> 
  <port /> 
  <data>-SA</data> 
  </probeName>
- <managedEntity>
  <name>TestBox</name> 
- <attributes>
  <attribute name="Business Unit">UNKNOWN</attribute> 
  <attribute name="Department">UNKNOWN</attribute> 
  <attribute name="Team">UNKNOWN</attribute> 
  <attribute name="Environment">UNKNOWN</attribute> 
  <attribute name="ServerModel">UNKNOWN</attribute> 
  <attribute name="datacentre">UNKNOWN</attribute> 
  <attribute name="Application">UNKNOWN</attribute> 
  <attribute name="Description"UNKNOWN</attribute> 
  </attributes>
  <types>
  </types>
    </managedEntity>
    <gateways>
    </gateway>
    </gateways>
  </selfAnnounce>
</netprobe>

How do I add the data? I thought it would be something like this which I would run for each attribute name

[xml]$XML = Get-Content C:\selfannounce.xml
$tempchild = $xml.CreateElement("attribute")
$tempchild.InnerText = "Unknown"
$a = $XML.SelectSingleNode("//attributes")
$a.AppendChild($tempchild)

[xml]$XML.Save("C:\selfannouncetest.xml")

But this gives me the below.

  <attributes>
    <attribute>Unknown</attribute>
  </attributes>

I dont know how to get the "attribute name=" part.

<attribute name="Business Unit">UNKNOWN</attribute> 

Anyone know the best way to achieve this? It will be added to the following code

Try {
$API = New-Object System.Net.WebClient
$APIData = $API.DownloadString($FullURL)
Set-Content -Value $APIdata -Path $APIDataFile -Force
#$Newdata = Get-Content C:\API.txt
}
catch [Net.WebException] {
$CheckAPIFile = Test-Path $APIDataFile 
If ($CheckAPiFile -eq $true){
$APIdata = Get-Content $APIDataFile
}
Else {

#Create attributes and add UNKNOWN

} 
}

And also, how can I add the below to the logic if the API call is successful or text file exists? Not sure where to insert it in the above script

[xml]$XML = Get-Content $SelfannounceXMLEdit
($APIData -split "`n") | Where-Object { $_.Trim() } | ForEach-Object {
    $tempchild = [xml]$_.Trim()
    $attribute = $xml.ImportNode($tempchild.attribute, $true)
    $newType = $XML.netprobe.selfAnnounce.managedEntity.selectsinglenode("attributes").AppendChild($attribute)
}
$XML.Save($SelfannounceXMLEdit)

Please note, must work on PowerShell 2.0.

Upvotes: 0

Views: 115

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

Use the SetAttribute() method:

$tempchild = $xml.CreateElement("attribute")
$tempchild.InnerText = "Unknown"

$tempchild.SetAttribute("name","Name value goes here")

$a = $XML.SelectSingleNode("//attributes")
$a.AppendChild($tempchild)

Upvotes: 1

Related Questions