4 Replies Latest reply on Jul 17, 2015 2:41 PM by gheorghe.vatui

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

    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.

        • Re: Powershell tools 1.1.0.0 cannot get back a new AF Element it just created
          dng

          Hi Gheorghe,

           

          The cmdlet

          $newElement = Get-AFElement -Name $name -AFDatabase $getAFDatabase
          

          returns only the root element under your AF Database.

           

          After you created a child element using

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

           

          You can get the same child element by specifying the parent element instead of the AF database

          $newElement = Get-AFElement -AFElement $parentElement -Name $name
          

           

          Alternatively, you can store the AFElement when you create it:

          $newElement = Add-AFElement -Name $name -ElementTemplate $template -ReferenceType $referenceType -AFElement $parentElement
          

           

          and use it in subsequent operations.

           

          Hope it helps. Please let me know if you have any questions. Thanks.

          1 of 1 people found this helpful
            • Re: Powershell tools 1.1.0.0 cannot get back a new AF Element it just created
              gheorghe.vatui

              Hi Daphne,

               

              I changed my code to use instead of these tree lines:

               

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

               

              these new ones:

               

                  $reloadedAFServer = Get-AFServer -Name $PIAFServerName -ErrorAction Continue
                  $reloadedAFDatabase = Get-AFDatabase -Name $PIAFDatabaseName -AFServer $reloadedAFServer
                  $reloadedParentElement = Get-AFElement -Name $rootElementName -AFDatabase $reloadedAFDatabase
                 

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

               

              Essentially I am reloading the server, database and parent element (all these are found). Then I am trying to reload the newly created Element - this still fails:

               

              Get-AFElement : Parameter set cannot be resolved using the specified named parameters.

              +         $newElement = Get-AFElement -AFElement $reloadedParentElement -Name $nam ...

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

                  + CategoryInfo          : InvalidArgument: (:) [Get-AFElement], ParameterBindingException

                  + FullyQualifiedErrorId : AmbiguousParameterSet,OSIsoft.PowerShell.GetAFElement

               

               

              Trying the second suggestion worked - i.e. storing the newly created element in a variable (and later on passing it around to other functions):

               

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

                • Re: Powershell tools 1.1.0.0 cannot get back a new AF Element it just created
                  dng

                  Hi Gheorghe,

                   

                  The reason you were getting the ParameterBindingException is because you are specifying both -AFDatabase and -AFElement.

                  As I mentioned, Get-AFElement will return the root element under your AFDatabase if you specify -AFDatabase, and will return the root element under the specified parent element if you specify -AFElement. With the following code:

                   

                  $newElement = Get-AFElement -AFElement $reloadedParentElement -Name $name -AFDatabase $reloadedAFDatabase -ErrorAction Stop
                  

                   

                  The statement is specifying two "parent" elements for Get-AFElement to look at, which is ambiguous. (You can think of the parameter -AFElement and -AFDatabase as mutually exclusive.)

                   

                  Please use the following statement to get the $newElement:

                   

                  $newElement = Get-AFElement -AFElement $reloadedParentElement -Name $name -ErrorAction Stop
                  

                   

                  One addition note is that there is no need to reload the AF server before attempting to get the new element after creation in the PowerShell script.