James Devine

Search For Elements By Element Template Using PowerShell

Blog Post created by James Devine on Sep 29, 2017

At the time of this blog post there is no parameter built into the OSIsoft library of PowerShell commands to search for elements generated using a specific template. If your element tree is complex with varied levels and parent elements throughout your element structure you may need a way to quickly locate and identify the elements generated using a specific template. Here is a code sample that worked for me:


<#region Descriptor:

    This script connects to an af server (pi system), loops through elements, then outputs

    a list of AF elements generated using a specific element template, and then disconnects.








# Connect to Target AF Server:

     $myAFServer = Get-AFServer -Name $afSrvNAME                          #~ Explicitly identify target AF Server

# OPTIONAL: $afConn = Get-AFServer -Default                               #~ Connects to the Default AF Server

     $afConn = Connect-AFServer -AFServer $myAFServer


# Generate Global Variable for Target AF Database (later in code known more simply as '$afDatabase'):

    $global:afDatabase = Get-AFDatabase -Name $afDBName -AFServer $myAFServer


# Generate Global Variable for Target Element Template:

    $global:afElementTemplate = Get-AFElementTemplate -Name $afElemTemplateName -AFDatabase $afDatabase

    $global:targetTemplate = $afElementTemplate.Name


# Loop Through All The Elements To Locate Those Generated Using The Target Template:

    $afElements = Get-AFElement -AFDatabase $afDatabase  #~ This loops through the parent elements

    foreach ($afElement in $afElements){

        #~ Pass This Element To the Primary Function Below:




    That's it! - - The rest of the code is the pair of repeating functions to find elements that have children

    until you reach the elements that match your target template.



# Primary Function To Check for Elements Generated Using The Target Template:

    function ChildElementsQuest01 ($testElement){

        $thisChildElements = $testElement.Elements

        foreach ($thisChildElement in $thisChildElements){

            # Test if this element has children AND the template is not our target template:

            if($thisChildElement.Elements.Count -gt 0 -and $thisChildElement.Template.Name -ne $targetTemplate) {

                # this element has children and it was not generated using the target template,

                # so we pass it to the secondary check function for more processing



            # Otherwise there are no children and still need to check if the element was generated using the target template:

            else {

                if($thisChildElement.Template.Name -eq $targetTemplate){

                    <# ---  Y O U R  C O D E  S T A R T S    H E R E    --- #>


                    Write-Host $thisChildElement.Name "was generated using the" $thisChildElement.Template.Name "element template."


                    <# ---  Y O U R  C O D E  E N D S    H E R E    --- #>






# Secondary Function to Cycle Through Child Elements of Element Sent Here From The Primary Function:

    # elements only get passed here if they have children AND were not generated using the target template.

    # The idea is to process all of its child elements using the the primary function (ChildElementsQuest01 above)

    function ChildElementsQuest02 ($testElement){

        $thisChildElements = $testElement.Elements

        foreach($thisChildElement in $thisChildElements){





# The last step is to disconnect from the AF Server:

    Disconnect-AFServer -AFServer $myAFServer