XML {$_.contains with -and

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.

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

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!

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

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'
}