AnsweredAssumed Answered

Powershell tools 1.1.0.0 cannot get back a new AF Element it just created

Question asked by gheorghe.vatui on Jul 15, 2015
Latest reply on Jul 17, 2015 by gheorghe.vatui

I was investigating the creation of PI AF Elements using Powershell.

 

For example, I am creating an new Element "PI-Buffer Server" under the parent Element "Interfaces" .

The new Element is based on an Element Template.

 

Function Create AFElement below is creating the new element:

 

 

function createAFElement ($name, $template, $referenceType, $parentElement){

       

        # create Af Element under the root node

        Add-AFElement -Name $name -ElementTemplate $template -ReferenceType $referenceType -AFElement $parentElement -ErrorAction Stop

 

        # No need to add the Attributes since using the Element template which should have all needed attribute templates

        # need to update Attributes only

 

        $getAFServer = Get-AFServer -Name $PIAFServerName -ErrorAction Continue

        $getAFDatabase = Get-AFDatabase -Name $PIAFDatabaseName -AFServer $getAFServer

        $newElement = Get-AFElement -Name $name -AFDatabase $getAFDatabase -ErrorAction Stop  #FAILS !!!!!

 

        updateAFElement $newElement

 

    }

 

 

Now when I get to the line :

 

    $newElement = Get-AFElement -Name $name -AFDatabase $getAFDatabase -ErrorAction Stop  #FAILS !!!!!

 

Powershell cannot find the Element it just created  - error:

 

Get-AFElement : AF Element PI-Buffer Server not found in database <myDB>.

 

+         $newElement = Get-AFElement -Name $name -AFDatabase $getAFDatabase -Erro ...

+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Elements:AFElements) [Get-AFElement], GetPIElementException

    + FullyQualifiedErrorId : NameNotFound,OSIsoft.PowerShell.GetAFElement

 

 

I also tried to disconnect and reconnect from the AF server hoping this would force Powershell to reload its cache.

Unfortunately it did not help... Note that if I refresh a previously open PI System Explorer, PSE does show me the new Element.

 

function createAFElement ($name, $template, $referenceType, $parentElement){
  
    # create Af Element under the root node
    Add-AFElement -Name $name -ElementTemplate $template -ReferenceType $referenceType -AFElement $parentElement -ErrorAction Stop

 

    # No need to add the Attributes since using the Element template which should have all needed attribute templates
    # need to update Attributes only
  
  
    # disconnect from AF Server to force cache update
    $foundPIAFSrv = Get-AFServer -Name $PIAFSrv
    Disconnect-AFServer -AFServer $foundPIAFSrv -ErrorAction Continue

 

    # reconnect
    try{
  
        # connect to AF if not already connected
        $foundPIAFSrv = Get-AFServer -Name $PIAFSrv
        $errVar = @()

 

        Connect-AFServer -AFServer $foundPIAFSrv -ErrorVariable errVar -ErrorAction Continue
   
    }
    catch{
      
        Echo "Error: Failed to connect to AF Server. Aborting ... Error details:`r`n$errVar[0]" >> $logFile
        Exit
    }

 

    $getAFServer = Get-AFServer -Name $PIAFServerName -ErrorAction Continue
    $getAFDatabase = Get-AFDatabase -Name $PIAFDatabaseName -AFServer $getAFServer
    $newElement = Get-AFElement -Name $name -AFDatabase $getAFDatabase -ErrorAction Stop  #FAILS !!!!!

 

    updateAFElement $newElement

 

}

How can I force Powershell to refresh the AF database and get the new Element ?

 

I need to pass the new Element to  another function which updates its Attributes.

Outcomes