I have an XML file which contains;
<?xml version="1.0"?>
<JobContainer version="2017-1">
<Object name="MainObject" type="TDM_Container">
<Object name="OrderList" type="TDM_List_Order">
<List name="Items">
<Object type="TDM_Item_Order">
<Property name="IntOrderID" value="3I-390049-SZEPLOUSKI-793269"/>
</Object>
</List>
</Object>
<Object name="ModelJobList" type="TDM_List_ModelJob">
<List name="Items">
<Object type="TDM_Item_ModelJob">
<Property name="ModelJobID" value="MJ5393C2FF84844DE38BC481CB5AD36D93"/>
</Object>
</List>
</Object>
<Object type="TDM_Item_ModelElement">
<Property name="ModelJobID" value="MJ5393C2FF84844DE38BC481CB5AD36D93"/>
<Property name="ProcessStatusID" value="psScanned"/>
<Property name="ProcessLockID" value="plReady"/>
<Property name="ManufacturingProcessID" value="57435_ManufacturingProcess17"/>
<Property name="ManufacturerID" value="27606"/>
</Object>
<Object type="TDM_Item_ModelElement">
<Property name="ModelJobID" value="MJ5393C2FF84844DE38BC481CB5AD36D93"/>
<Property name="ProcessStatusID" value="psClosed"/>
<Property name="ProcessLockID" value="plReady"/>
<Property name="ManufacturingProcessID" value="27606_ManufacturingProcess8"/>
<Property name="ManufacturerID" value="27606"/>
</Object>
<Object type="TDM_Item_ModelElement">
<Property name="ModelJobID" value="MJ5393C2FF84844DE38BC481CB5AD36D93"/>
<Property name="ProcessStatusID" value="psScanned"/>
<Property name="ProcessLockID" value="plReady"/>
<Property name="ManufacturingProcessID" value="57435_ManufacturingProcess17"/>
<Property name="ManufacturerID" value="27606"/>
</Object>
</List>
</Object>
<Object name="ElementList" type="TDM_List_Element">
<List name="Items">
<Object type="TDM_Item_Element">
<Property name="Anatomical" value="False"/>
</Object>
</List>
</Object>
</Object>
</JobContainer>
I need to find the the ManufacturingProcessID property with value = "27606_ManufacturingProcess8", and if I find it then look at the ProcessStatusID property under the same node, and if the value = "psClosed" then I need to move the xml file to another folder.
My code below worked, but it only worked when the node was the first. But sometimes the node is not the first node. How do I find this node if it is not the first node.
Get-ChildItem $sSourceFolder | ForEach-Object -Process {
if ($_.PSIsContainer)
{
# Store subfolder path in a variable
$sFolderPath = $_.FullName
$sFolderName = Split-Path $sFolderPath -Leaf
Get-ChildItem $sFolderPath | Where {$_.Name -like $sFolderName + '.xml'} | foreach{
$sFilepath = $_.FullName
[xml]$xml2 = Get-Content $sFilepath
$sValueMPI = $xml2.SelectNodes('//Property') | ?{$_.name -eq "ManufacturingProcessID"} | select -First 1 -ExpandProperty value
if ($sValueMPI -eq '27606_ManufacturingProcess8')
{
$sValue = $xml2.SelectNodes('//Property') | ?{$_.name -eq "ProcessStatusID"} | select -First 1 -ExpandProperty value
if ($sValue -eq 'psClosed')
{
# If destination folder already exists, add sequential suffix like (1), (2), etc.
if (Test-Path ($sDestFolder + $sFolderName))
{
$j = 1
While (Test-Path ($sDestFolder + "$sFolderName($j)"))
{
$j = $j + 1
}
$sFolderName = "$sFolderName($j)"
}
# Move folder to archive destination
Move-Item $sFolderPath ($sDestFolder + $sFolderName) -ErrorVariable MoveError -Verbose -Force *>> $sLogPath
if (!($MoveError)) {$i = $i + 1} #if no move error, then increment counter
}
}
}
}
}