XML {$_.contains with -and

Welcome Forums General PowerShell Q&A XML {$_.contains with -and

  • This topic has 3 replies, 2 voices, and was last updated 2 months ago by
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #199883
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      Hello,

      I’m searching an XML document to see if it contains  X string -and Y string but I can’t get it to work properly.  This is what I started with:

      if (Get-Content -Path "C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml" | Where-Object {$_.contains("\\shares\path\longpath\LocalData\reallylongpath\DesignFiles\") -and ("C:\Program Files (x86)\Application\DentalDesigner1\")}) {
      Write-Host "#1"
      }
      if (Get-Content -Path "C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml" | Where-Object {$_.contains("\\shares\path\longpath\LocalData\reallylongpath\DesignFiles\") -and ("C:\Program Files (x86)\Application\DentalDesigner2\")}) {
      Write-Host "#2"
      }
      if (Get-Content -Path "C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml" | Where-Object {$_.contains("\\shares\path\longpath\LocalData\reallylongpath\DesignFiles\") -and ("C:\Program Files\Application\DentalDesigner3\")}) {
      Write-Host "#3"
      }

       

      I’ve tried adding various {} and () but it still doesn’t work.  It either writes host #1, #2 and #3 or doesn’t write anything at all.
      Other things I’ve tried:

      if (Get-Content -Path "C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml" | Where-Object {$_.contains("\\shares\path\longpath\LocalData\reallylongpath\DesignFiles\" -and "C:\Program Files\Application\DentalDesigner3\")}) {
      Write-Host "#3"
      }
      
      if (Get-Content -Path "C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml" | Where-Object {$_.contains("\\shares\path\longpath\LocalData\reallylongpath\DesignFiles\")} -and {$_.contains("C:\Program Files\Application\DentalDesigner3\")}) {
      Write-Host "#3"
      }

       

      I know contains is kind of finicky, I’m not even sure it’s possible with this term/syntax.  Thanks in advance for your help.

    • #199907
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      XML files are not simple text files. They have a structure and a syntax and if they are valid XML files you should treat them as such. You should use something like this:

      [xml]$XmlDocument = Get-Content -Path "C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml"

      … and you should do it only once … not more. Now you have the XML structure in the variable $XmlDocument and can work with it. You might start to read up more about dealing with XML files with Powershell here: https://www.business.com/articles/powershell-read-xml-files.

      -Contains looks for an element in an array of elements. And this has to be a perfect match.

      'AB','BC','CD','DE' -contains 'B'

      … even if “B” is present in the first and in the second element -Contains returns “$false“.

    • #200729
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      Thank you Olaf for your detailed explanation.  You sure sent me down a rabbit hole, but it’s all for the better.  I’ve read the documentation you linked and watched a few videos.  Here is what I came up with.

       

      [xml]$XmlDocument = Get-Content -Path "C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml"
      $XmlFileShare = ((($XmlDocument.ControlPanelCustomization).Object).Object | ? {$_.name -match "OrderDirectory"} | Select-Object -Property Property -ExpandProperty Property) | Select-Object -Property Value -ExpandProperty Value | Select -First 1
      $XmlFFolderDirectory = ((($XmlDocument.ControlPanelCustomization).Object).Object | ? {$_.name -match "DentalDesigner2010Dir"} | Select-Object -Property Property -ExpandProperty Property) | Select-Object -Property Value -ExpandProperty Value | Select -First 1
      if ($XmlFileShare -eq "\\Server\Shares\PathImLookingFor\DesignFiles\" -and $XmlFFolderDirectory -eq "C:\Program Files (x86)\Application\DentalDesigner2010-1\") {Write-Host "True"}
      else {Write-Host "I Suck"}

       

      In the end, I will replace write-host with Copy-Item and this will search for strings within the XML config file and replace it with a new one if those variables turn up “True”.

       

      Thank you again!

    • #200768
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      Thank you Olaf for your detailed explanation. You sure sent me down a rabbit hole, but it’s all for the better. I’ve read the documentation you linked and watched a few videos.

      I’m proud of you that you learned and managed to get what you needed. Great. 🙂 Here is some more stuff to read and make your scripts even better: The Unofficial Powershell Best Practices and Style Guide. 😉

      With a little formatting and avoiding aliasses your code would gain better readability and would be easier to maintain, I think.

      [xml]$XmlDocument = Get-Content -Path 'C:\Application Configuration\DentalSystem\Dental System Control Panel\ControlPanelCustomization.xml'
      $XmlFileShare = ((($XmlDocument.ControlPanelCustomization).Object).Object | 
          Where-Object { $_.name -match 'OrderDirectory' } | 
              Select-Object -Property Property -ExpandProperty Property) | 
                  Select-Object -Property Value -ExpandProperty Value | 
                      Select-Object -First 1
      $XmlFFolderDirectory = ((($XmlDocument.ControlPanelCustomization).Object).Object | 
          Where-Object { $_.name -match 'DentalDesigner2010Dir' } | 
              Select-Object -Property Property -ExpandProperty Property) | 
                  Select-Object -Property Value -ExpandProperty Value | 
                      Select-Object -First 1
      if (
          $XmlFileShare -eq '\\Server\Shares\PathImLookingFor\DesignFiles\' -and 
          $XmlFFolderDirectory -eq 'C:\Program Files (x86)\Application\DentalDesigner2010-1\'
      ) { 
          Write-Host 'True'
      }
      else { 
          Write-Host 'I Suck'
      }
Viewing 3 reply threads
  • You must be logged in to reply to this topic.