2016-05-19 15 views
1

私はローカルホスト名を挿入するXMLを持っています。以下のスクリプトは、XMLへの正しいゲートウェイを追加する必要があるホストの名前に依存ワイルドカードの使い方

<name>lonmq1111</name> 
<name>stoms1111</name> 
<name>bqqlk1111</name> 
<name>hkgtp1111</name> 

:ホスト名は、以下のいずれかである可能性があります。例:

IF <name>lon*<name> OR <name>sto*<name> THEN 
    add these gateways 
ELSEIF <name>bqq*</name> OR <name>hkg*</name> THEN 
    add different gateways 
ELSEIF etc. 

私は以下のとおりですが、動作しません。途中でワイルドカードを使用する方法に関するアイデアはありますか?

$file = Get-Content C:\testnew.xml 
if ($file -like ' <name>lon*</name>' -or '<name>sto*</name>') { 
    # load XML file 
    [xml]$doc = Get-Content "C:\testnew.xml" 

    # create node <hostname> 
    $comp = $doc.CreateNode('element', 'hostname', '') 
    $desc = $doc.CreateTextNode('test') 
    $comp.AppendChild($desc) 

    # create node <port> 
    $sref = $doc.CreateNode('element', 'port', '') 
    $desc = $doc.CreateTextNode('1111') 
    $sref.AppendChild($desc) 

    # create node <gateway> and append child nodes <hostname> and <port> 
    $src = $doc.CreateNode('element', 'gateway', '') 
    $src.AppendChild($comp) 
    $src.AppendChild($sref) 

    # append node <gateway> to node <gateways> 
    $svc = $doc.SelectSingleNode('//gateways') 
    $svc.AppendChild($src) 

    # create node <hostname> 
    $comp = $doc.CreateNode('element', 'hostname', '') 
    $desc = $doc.CreateTextNode('test2') 
    $comp.AppendChild($desc) 

    # create node <port> 
    $sref = $doc.CreateNode('element', 'port', '') 
    $desc = $doc.CreateTextNode('2222') 
    $sref.AppendChild($desc) 

    # create node <gateway> and append child nodes <hostname> and <port> 
    $src = $doc.CreateNode('element', 'gateway', '') 
    $src.AppendChild($comp) 
    $src.AppendChild($sref) 

    # append node <Source> to node <Service> 
    $svc = $doc.SelectSingleNode('//gateways') 
    $svc.AppendChild($src) 

    # save XML file 
    $doc.Save("C:\testnew.xml") 
} 

私はちょうど*lon*を使用して<name>を除去するが、XMLでの他のすべてが自動的に移入されことができます。私は、避けたい文書に*bqq**lon*があるシナリオで終わることができます。注 - これはpowershell v2.0で動作する必要があります。編集されます

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>lonms1122</name> 
     <attributes> 
     </attributes> 
     <types> 
     </types> 
    </managedEntity> 
    <gateways> 
     <gateway> 
     </gateway> 
    </gateways> 
    </selfAnnounce> 
</netprobe> 

@Mathias

あなたの答えは素晴らしい作品。あなたの答えの下の部分をテストするには

$doc = [xml](Get-Content C:\selfannouncetestnew.xml) 

$gateway = switch -Wildcard($doc.SelectSingleNode('//managedEntity/name').InnerText) 
{ 
    "lon*" { 
# create node <hostname> 
$comp = $doc.CreateNode('element', 'hostname', '') 
$desc = $doc.CreateTextNode('test') 
$comp.AppendChild($desc) 

# create node <port> 
$sref = $doc.CreateNode('element', 'port', '') 
$desc = $doc.CreateTextNode('1111') 
$sref.AppendChild($desc) 

# create node <gateway> and append child nodes <hostname> and <port> 
$src = $doc.CreateNode('element', 'gateway', '') 
$src.AppendChild($comp) 
$src.AppendChild($sref) 

# append node <gateway> to node <gateways> 
$svc = $doc.SelectSingleNode('//gateways') 
$svc.AppendChild($src) 

# create node <hostname> 
$comp = $doc.CreateNode('element', 'hostname', '') 
$desc = $doc.CreateTextNode('test2') 
$comp.AppendChild($desc) 

# create node <port> 
$sref = $doc.CreateNode('element', 'port', '') 
$desc = $doc.CreateTextNode('2222') 
$sref.AppendChild($desc) 

# create node <gateway> and append child nodes <hostname> and <port> 
$src = $doc.CreateNode('element', 'gateway', '') 
$src.AppendChild($comp) 
$src.AppendChild($sref) 

# append node <gateway> to node <gateways> 
$svc = $doc.SelectSingleNode('//gateways') 
$svc.AppendChild($src) 

$doc.Save("c:\selfannouncetestnew.xml") 

    } 

    "sto*" { 
    # create node <hostname> 
$comp = $doc.CreateNode('element', 'hostname', '') 
$desc = $doc.CreateTextNode('test3') 
$comp.AppendChild($desc) 

# create node <port> 
$sref = $doc.CreateNode('element', 'port', '') 
$desc = $doc.CreateTextNode('3333') 
$sref.AppendChild($desc) 

# create node <gateway> and append child nodes <hostname> and <port> 
$src = $doc.CreateNode('element', 'gateway', '') 
$src.AppendChild($comp) 
$src.AppendChild($sref) 

# append node <gateway> to node <gateways> 
$svc = $doc.SelectSingleNode('//gateways') 
$svc.AppendChild($src) 

# create node <hostname> 
$comp = $doc.CreateNode('element', 'hostname', '') 
$desc = $doc.CreateTextNode('test4') 
$comp.AppendChild($desc) 

# create node <port> 
$sref = $doc.CreateNode('element', 'port', '') 
$desc = $doc.CreateTextNode('4444') 
$sref.AppendChild($desc) 

# create node <gateway> and append child nodes <hostname> and <port> 
$src = $doc.CreateNode('element', 'gateway', '') 
$src.AppendChild($comp) 
$src.AppendChild($sref) 

# append node <gateway> to node <gateways> 
$svc = $doc.SelectSingleNode('//gateways') 
$svc.AppendChild($src) 

$doc.Save("c:\selfannouncetestnew.xml")  

    } 

    "bqq*" { 
     "barragw:3456" 
    } 

    "hkg*" { 
     "hongkonggw:4567" 
    } 

    default { 
     "defaultgw:5678" 
    } 
} 

$hostname,$port = $gateway -split ':' 
# create node <hostname> 
$comp = $doc.CreateNode('element', 'hostname', '') 
$desc = $doc.CreateTextNode($hostname) 
$comp.AppendChild($desc) 

# create node <port> 
$sref = $doc.CreateNode('element', 'port', '') 
$desc = $doc.CreateTextNode($port) 
$sref.AppendChild($desc) 


$doc.Save("C:\selfannouncetestnew.xml") 

以下のコードを参照してください、私はhkggk1122に名前を変えたが、何も起こりません。名前がlon(something)またはsto(something)の場合、ゲートウェイが追加されます。私はあなたのソリューションを稼働させたいと思っていますが、あなたがボトム部分で何をしているのかを確かめてはいけません。

+0

あなたはおそらくだろう既存のXMLを解析し、XPathを使用して関連する値を持つ名前ノードを見つけ出す方が効果的です。 xmlドキュメント構造全体を見せてもらえますか? –

+0

@Mathias、更新を参照してください。私はXMLを含んでいます。ここには、それがどのホストに依存しているのかが自動的に入り込みます。 –

+1

ちょうどメモとして:$ var-like 'A'または 'B''はあなたが期待しているように動作しません。式のロジックは、 '($ var-like 'A') - または( 'B')'のように動作します。 '' B''は空でない文字列なので、ブール演算で '$ true'と評価されます。ブールOR演算は、その句の1つが真であれば真と評価されるので、式全体は常に '$ true'です。 '$ var-like 'A' - または$ var-like 'B''のように、変数を各文字列と個別に比較して結果を得る必要があります。 –

答えて

2

:で

' <name>lon*</name>' 

を交換し

$xml = [xml](Get-Content C:\testnew.xml) 

$gateway = switch -Wildcard($xml.SelectSingleNode('//managedEntity/name').InnerText) 
{ 
    "lon*" { 
     "londongw:1234" 
    } 

    "sto*" { 
     "stockholmgw:2345" 
    } 

    "bqq*" { 
     "barragw:3456" 
    } 

    "hkg*" { 
     "hongkonggw:4567" 
    } 

    default { 
     "defaultgw:5678" 
    } 
} 

$hostname,$port = $gateway -split ':' 

# Create appropriate childnodes and append here 
# create node <hostname> 
$comp = $doc.CreateNode('element', 'hostname', '') 
$desc = $doc.CreateTextNode($hostname) 
$comp.AppendChild($desc) 

# create node <port> 
$sref = $doc.CreateNode('element', 'port', '') 
$desc = $doc.CreateTextNode($port) 
$sref.AppendChild($desc) 

# and so on ... 
+0

私はスイッチを使用して私のシナリオでそれを使う方法を理解しようとしています。あなたの例が助けてくれました、ありがとう。しかし、私はまだスイッチがどのように機能するかはまだ分かりません。名前が孤独なものなら、{}の中のすべてを実行しますか?最後の行は何ですか? –

+0

'name'ノード内のテキストが' -like lon * 'ならば、' {} 'の中のコードが実行されるので' $ gateway'は '' londongw:1234 "'に割り当てられますそれ。 'default'ブロックは、一致するものが見つからない場合に実行されます。もしリストにある4つのオプションだけが存在することが絶対確実であれば、 'default'ブロックを削除することができます。 –

+0

' $ hostname、$ port = $ gateway -split ': '' do? –

0

likeパラメータには空白があります。 managementEntity/nameノードを検索し、ゲートウェイの詳細を決定するswitchを使用し、アップフロント文書全体を解析し

'<name>lon*</name>' 
+0

も注目されています。しかしそれは違いをもたらさなかった。スクリプトはゲートウェイの追加を続けます。 –

関連する問題