How to read an OPC item value in PowerShell

From OPC Labs Knowledge Base
Revision as of 05:20, 9 September 2016 by User (talk | contribs)

The following PowerShell code reads an OPC item and displays its value:

Add-Type -Path OpcLabs.EasyOpcClassic.dll

# Create EasyOPC-DA component 
$client = New-Object OpcLabs.EasyOpc.DataAccess.EasyDAClient

# Read item value and display it
$client.ReadItemValue("", "OPCLabs.KitServer.2", "Demo.Single")

We have tested this with QuickOPC 5.40 and PowerShell 3.0. Presumably the same concept works with OPC UA (Unified Architecture) too.

For now, consider this just a proof of concept. So far, we have not specifically targeted PowerShell when designing the components, and there are some rough edges. We have found following issues so far:

  1. You need to specify a full path to the QuickOPC assemblies, or copy them over (from Assemblies\net452 folder) to your current directory (this is what we have done for the example code above).
  2. First time the Add-Type is executed, it throws a non-terminating error, indicating a problem loading the assembly. But the execution continues and the read works well. This has to do with the way we have embedded some dependent assemblies, in order to reduce the number of files the developer needs to deal with.

Contact us should you need further help developing a PowerShell solution with QuickOPC.


Links related to/used when writing this article: