Powershell:将元素从一个阵列复制到另一个阵列

时间:2020-08-07 08:15:18

标签: arrays powershell xpath

嗨,在我的第一个数组中,有一个来自xml的节点。

$result=Select-xml -xml $uar -xpath "//test:UAVariable[contains(@NodeId,'ns=1;s=::')][starts-with(@DataType,'i=')]" -namespace $ns  | select -ExpandProperty node

现在我必须像这样进一步过滤。然后,如果两个都为真,则从我的数组中删除该元素(如果可能),或将该元素复制到新元素中。

$result | foreach {
    $AttExists = $_.Arraydimensions
    $NodeExists = $_.References.Reference.ReferenceType
    if ($AttExists){
        if ($NodeExists -eq 'HasComponent'){            
         #if this is happening i want to delete this hole Element (Node) in my array is this possible?   
        }
        else{
         #if deleting is not possible i want to copy these Element (Node) into a new array is this possible?
        }
    }
}

尝试了以下几种方法: $_=$null或类似 $newArray[$i] = $result[$j] $i++的计数器 但是,它不会删除或复制任何内容。有人能帮我吗? xml数据:

<?xml version="1.0" encoding="utf-8"?>
<UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns:ua="http://xxx/NodeSet.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pv="http://yyy/NodeSet.xsd" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd">
    <UAObject NodeId="ns=1;s=::" BrowseName="1:::">
        <DisplayName>&lt;Default&gt;</DisplayName>
        <References>
            <Reference ReferenceType="HasTypeDefinition">ns=2;i=10001</Reference>
            <Reference ReferenceType="Organizes" IsForward="false">ns=2;i=20001</Reference>
            <Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV</Reference>
            <Reference ReferenceType="Organizes">ns=1;s=::Program</Reference>
        </References>
        <Extensions>
            <Extension>
                <pv:ObjectExtension>
                    <ACL>
                        <ACE Role="1" Allow="0x017F"/>
                        <ACE Role="2" Allow="0x015F"/>
                    </ACL>
                </pv:ObjectExtension>
            </Extension>
        </Extensions>
    </UAObject>
    <UAObject ParentNodeId="ns=1;s=::" NodeId="ns=1;s=::AsGlobalPV" BrowseName="1:Global PV">
        <DisplayName>Global PV</DisplayName>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=61</Reference>
            <Reference ReferenceType="Organizes" IsForward="false">ns=1;s=::</Reference>
            <Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV:gFahrzeug</Reference>
            <Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV:gLebewesen</Reference>
        </References>
    </UAObject>
    <UAVariable DataType="ns=1;i=100000" ParentNodeId="ns=1;s=::AsGlobalPV" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug" BrowseName="1:gFahrzeug" AccessLevel="3" ValueRank="-1">
        <DisplayName>gFahrzeug</DisplayName>
        <References>
            <Reference ReferenceType="HasTypeDefinition">ns=1;i=100005</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug</Reference>
        </References>
        <Extensions>
            <Extension>
                <pv:VariableExtension AuditEvents="true">
                    <Value>
                        <Binding Type="PV" Target="::gFahrzeug"/>
                    </Value>
                </pv:VariableExtension>
            </Extension>
        </Extensions>
    </UAVariable>
    <UAVariable DataType="ns=1;i=100010" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug" BrowseName="1:Kraftfahrzeug" AccessLevel="3" ValueRank="-1">
        <DisplayName>Kraftfahrzeug</DisplayName>
        <References>
            <Reference ReferenceType="HasTypeDefinition">ns=1;i=100015</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.PKW</Reference>
        </References>
        <Extensions>
            <Extension>
                <pv:VariableExtension AuditEvents="true">
                    <Value>
                        <Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug"/>
                    </Value>
                </pv:VariableExtension>
            </Extension>
        </Extensions>
    </UAVariable>
    <UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" BrowseName="1:LKW" AccessLevel="3" ValueRank="1" ArrayDimensions="2">
        <DisplayName>LKW</DisplayName>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[0]</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[1]</Reference>
        </References>
        <Extensions>
            <Extension>
                <pv:VariableExtension AuditEvents="true">
                    <Value>
                        <Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW"/>
                    </Value>
                </pv:VariableExtension>
            </Extension>
        </Extensions>
    </UAVariable>
    <UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[0]" BrowseName="1:LKW[0]" AccessLevel="3">
        <DisplayName>LKW[0]</DisplayName>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
        </References>
        <Extensions>
            <Extension>
                <pv:VariableExtension AuditEvents="true">
                    <ACL>
                        <ACE Role="1" Allow="0x017F"/>
                        <ACE Role="2" Allow="0x015F"/>
                    </ACL>
                    <Value>
                        <Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW[0]"/>
                    </Value>
                </pv:VariableExtension>
            </Extension>
        </Extensions>
    </UAVariable>
    <UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[1]" BrowseName="1:LKW[1]" AccessLevel="3">
        <DisplayName>LKW[1]</DisplayName>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
        </References>
        <Extensions>
            <Extension>
                <pv:VariableExtension AuditEvents="true">
                    <ACL>
                        <ACE Role="1" Allow="0x017F"/>
                        <ACE Role="2" Allow="0x015F"/>
                    </ACL>
                    <Value>
                        <Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW[1]"/>
                    </Value>
                </pv:VariableExtension>
            </Extension>
        </Extensions>
    </UAVariable>
</UANodeSet>

代码:

[xml]$uar = Get-Content -Path 'C:\Users\strobel.ma\OneDrive - GEA\Desktop\XML to String\OpcUaMap(3)_28_07.uar'

$ns = New-Object System.Xml.XmlNamespaceManager($uar.NameTable) #asdf

$ns=@{GEA="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd";
    ua="http://br-automation.com/OpcUa/configuration/NodeSet.xsd";
    xsi="http://www.w3.org/2001/XMLSchema-instance";
    uax="http://opcfoundation.org/UA/2008/02/Types.xsd";
    xsd="http://www.w3.org/2001/XMLSchema";
    pv="http://br-automation.com/OpcUa/PLC/PV.xsd"}

$result=Select-xml -xml $uar -xpath "//GEA:UAVariable[contains(@NodeId,'ns=1;s=::')][starts-with(@DataType,'i=')]" -namespace $ns  | select -ExpandProperty node

$result | foreach {$_.NodeId = $_.NodeId -replace 'ns=1;s=::AsGlobalPV:'}

$result | foreach {$_.NodeId = $_.NodeId -replace 'ns=1;s=::'}

$result | foreach {
    $AttExists = $_.Arraydimensions
    $NodeExists = $_.References.Reference.ReferenceType
    if ($AttExists){
        if ($NodeExists -eq 'HasComponent'){            
         #if this is happening i want to delete this hole Element (Node) in my array is this possible?   
        }
        else{
         #if deleting is not possible i want to copy these Element (Node) into a new array is this possible?
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我建议您采用一种幼稚的方法,只需创建一个新的$ result2对象,即可在其中存储所需的内容

我猜默认的System.Array类型应该足够好:

声明新变量将项目存储在何处:

$result2 = @()

并通过在foreach循环中使用以下内容,将所需的内容存储在其中:

$result2 += $_