PI AF - Reset to Template in Bulk

Blog Post created by hanyong Employee on Jun 19, 2012

Recently I've been working on setting up several environments for demonstrations and testing purposes. To get a bit of head start on setting up a realistic PI AF model. I got a couple of databases from others so these can be imported into the AF Server.


Having the elements and the associated template created in my database from imported .xml file is great but in some situation that I still have to ensure that the attributes are getting the correct information. For example, attributes with PIPoint data reference should be pointing to the right PI server. Sometimes, even if the template of the attribute is configured using substitution parameters like %Server%, representing the name of PI AF Server, you can still end up in situations where the attribute instance is still pointing to another PI Server. This is because data reference configuration on an attribute can be locked in. An example would be if you use the option to Create or Update PI Point. After that, any subsequent changes made to the template will not propagate to elements based on the template.


This is where Reset to Template is useful. As the name suggests, it 'resets' the attribute to utilize the setting of the template. In PI System Explorer, you can select an attribute and choose the option to "Reset to Template", but the problem we have is there is no option to do that in bulk.


An alternative to do this in one step is using PI AF Builder add-in in Microsoft Excel. While there is no Reset to Template option, what you can do is copy the config string from the Attribute Template and replace the config strings of derived attribute. This is essentially what happens in the background when you choose to Reset to Template in PI System Explorer. This works but it may still take some effort, because you have to export the list of elements and attributes, update the config string and then export them back to the AF database.


Another alternative would be to do this using AF SDK, from a simple program, or a Powershell script. In my case, I've created a Powershell script that loads OSIsoft.AFSDK.dll, find all derived elements from an element template and resets all their attributes. All you need to provide is the AF Server name, AF Database name and name of element template.


The key part of resetting the attribute to template settings is the following:




    # * if the attribute have different data reference from the template, reset with template's data reference
    if($AFAttribute.Template.DataReferencePlugIn -ne $AFAttribute.DataReferencePlugIn)
        $AFAttribute.DataReferencePlugIn = $AFAttribute.Template.DataReferencePlugIn
    # * if attribute's data reference is not null and attribute's config string is difference from template. reset with template's config string
        if ($AFAttribute.ConfigString -ne $AFAttribute.Template.ConfigString)
            $AFAttribute.ConfigString = $AFAttribute.Template.ConfigString 
    # * if attribute's data reference is null, resetting means taking the template's default value
        $AFAttribute.SetValue($AFAttribute.Template.GetValue($null), $null)





With the script, the amount of manual work to reset the attributes to template is minimized. You can download this powershell script. There are some consideration that I have not included in the script that you want to take note of:

  • The script retrieves the element template by name, so you have to correctly input the name.
  • The script doesn't take into account child attributes.

Hope this would help anyone who does AF Database migration!