How to use wildcards?

advertisements

I have XML that inserts the local hostname. The hostname could be any of the below:

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

Dependent on the name of the host the script below needs to add the correct gateways to the XML. For example:

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.

I have the below, but it is not working. Any ideas on how to use wildcards in the middle?

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

I could just use *lon* and remove the <name> but everything else in the XML is auto populated. I could end up in the scenario where *bqq* and *lon* be in the document which I want to avoid. Note - this must work on powershell v2.0.

The XML that will be edited:

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

Your answer works great. See the code below

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

To test the bottom part of your answer, i changed the name to hkggk1122 but nothing happens. If the name is lon(something) or sto(something) it adds the gateways. I want to get your solution to work but not sure what you are trying to tell me do with the bottom part.


Parse the entire document up front, find the managementEntity/name node and use a switch to decide on the gateway details:

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