2016-05-18 110 views
0

私は、ホストの属性のAPIを呼び出すスクリプトの一部を持っています。 APIが失敗すると、(以前の実行からの)属性を含むテキストファイルがあるかどうかがチェックされます。ファイルが存在しない場合は、属性ノードにUNKNOWNを追加する必要があります。以下のXMLの期待されるアウトを参照してください。スクリプトはこの情報を入力するため、XMLは属性なしで開始されます。Powershell - XMLに新しいデータを追加

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

データを追加するにはどうすればよいですか?私はそれが私が各属性名のために動かすこのようなものだと思った。

[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") 

しかし、これは私に以下を与える。

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

「属性名=」の部分を取得する方法はわかりません。

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

誰でもこれを達成するための最良の方法を知っていますか?これは、次のコード

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 

} 
} 

に追加され、APIの呼び出しが成功するか、テキストファイルが存在する場合にも、どのように私はロジックに以下を追加することができますか?上記のスクリプトにどこに挿入するかわからない

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

PowerShell 2.0で作業する必要があります。

答えて

1

SetAttribute() methodを使用します。

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

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

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

働いたこと、私はそれをとても簡単なものを考えました。ヘルプをよろしくお願いいたします。それらのすべてのためにこれを行うための最善の方法は何ですか?リストを作成し、それぞれに対して作成するか、それぞれを個別に作成しますか? –

関連する問題