AnsweredAssumed Answered

How to create PI tag / PI Point from Powershell using PI SDK

Question asked by gheorghe.vatui on Nov 3, 2015
Latest reply on Nov 6, 2015 by gregor

Using Powershell 3.0

PI SDK 2014 (1.4.4.484)

 

I am trying to create a new PI tag using the PI SDK method "Add" from PIPoints Collection.

 

NOTE: I am aware the OSIsoft Powershell tools can do it, but in this case I can't use the tools.

 

I just can't get the syntax right ... please help me with an example  of Powershell code snippet..

 

I am first importing the PISDK, then creating an SDK object, then trying to create a new tag:

 

# Add reference to PISDK library

[reflection.assembly]::LoadWithPartialName("OSIsoft.PISDK") | Out-Null

[reflection.assembly]::LoadWithPartialName("OSIsoft.PISDKCommon") | Out-Null

[reflection.assembly]::LoadWithPartialName("OSIsoft.PITimeServer") | Out-Null

 

# Create a new PI SDK object

$PISDK2 = New-Object PISDK.PISDKClass

 

#building the attributes and Point type

 

$heartbeatTagName = MyTestHeartbeatTag

$heartbeatTagAttr =  @{"PointSource"="L";

                           "ptsecurity"="piadmin: A(r,w) | piadmins: A(r,w) | PIWorld: A(r)";

                           "datasecurity"="piadmin: A(r,w) | piadmins: A(r,w) | PIWorld: A(r)"}

 

 

    $ptType = $PISDK2.PIConstants.Item("PointTypeConstants").Item("Int 32").Value    # this returns 8

 

 

    $heartbeatTag = $PIServerHBObj.PIPoints.Add($heartbeatTagName, "classic", $ptType, $heartbeatTagAttr)

 

For some reason, no matter what I try for the last line, it never works, always complaining about some type mismatch.

(no specifics about the mismatch so I cannot figure it out).

 

I have also tried - in vain:

$PIServerHBObj.PIPoints.Add($heartbeatTagName, "classic",8, $heartbeatTagAttr)

$PIServerHBObj.PIPoints.Add($heartbeatTagName, "classic",8, )

$PIServerHBObj.PIPoints.Add($heartbeatTagName, "classic",8, $null)

$PIServerHBObj.PIPoints.Add($heartbeatTagName, "classic", $ptType, )

$PIServerHBObj.PIPoints.Add($heartbeatTagName, "classic",($PISDK2.PIConstants.Item("PointTypeConstants").Item("Int 32")), $heartbeatTagAttr)

 

AI assume "classic" should be ok since method expects a string here.

However, problem must be with the other two parameters, Pointtype and Point Attributes.

 

The PI SDK help mentions the PointType is a value from the PointTypeConstants enumeration - I am giving it the Value of $PISDK2.PIConstants.Item("PointTypeConstants").Item("Int 32") -- is that OK ..? What does it really expect, a number, a string...?

I could not find the NamedValues type in Powershell - I assume this is a hash table like the one I  use for $heartbeatTagAttr... would a hash table not work ?

 

The Add method creates a new PIPoint object and adds it to the collection. The method also creates a corresponding PI point on the collection’s parent Server.  It returns a reference to the newly created PIPoint object.

 

Syntax

object.Add Tagname, PointClass, PointType, [nvAttrs]

The Add method syntax has these parts:

 

Part

Description

object

An object expression that evaluates to a PIPoints object.

Tagname

A string expression which will become the name of the newly added PIPoint.

PointClass

A string expression which will become the point class of the newly added PIPoint.

PointType

A value from the PointTypeConstants enumeration, which will become the point type of the newly added PI point.

nvAttrs

A NamedValues collection containing NamedValue objects whose name property represents an attribute and whose value property is a variant representing the desired value for the attribute. [Optional]

 

Thank you for your help

Outcomes