Professional OPC
Development Tools

logos

QuickOPC in PowerShell

QuickOPC makes it easy for you to integrate OPC client functionality into your PowerShell scripts. Reading a value from OPC Data Access or OPC Unified Architecture server, or writing a data value can be achieved in just several lines of code. You can also set up subscriptions and receive event notifications about data changes. The component interfaces are designed to hide the complexities of OPC from the developer.

QuickOPC Price List

Read More

Simple OPC UA PowerShell Example

The code below (in PowerShell) reads and displays a monitored item value, using QuickOPC.NET:

# Create EasyOPC-UA component
$client = New-Object EasyUAClient

# Read node value and display it
$client.ReadValue("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", "nsu=http://test.org/UA/Data/ ;i=10853")

The code below subscribes to changes of a monitored item value, and displays the value with each change:

# Create EasyOPC-UA component
$client = New-Object EasyUAClient

# Hook events
Register-ObjectEvent -InputObject $client -EventName DataChangeNotification -Action { Write-Host $EventArgs }

# Subscribe
$handle = $client.SubscribeDataChange("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", "nsu=http://test.org/UA/Data/ ;i=10853", 100)

# Process events
$stopwatch =  [System.Diagnostics.Stopwatch]::StartNew() 
while ($stopwatch.Elapsed.TotalSeconds -lt 20) {    
    Start-Sleep -Seconds 1
}

The PubSub variety of OPC UA (as opposed to client-server) uses message-oriented middleware to deliver the data. QuickOPC supports it as well. Code example is available in the documentation, and installed with the product.

OPC Data Access ("Classic")

The code below reads and displays an item value:

# Create EasyOPC-DA component
$client = New-Object EasyDAClient

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

This is how you can subscribe to changes of an item, and display the value with each change:

# Create EasyOPC-DA component
$client = New-Object EasyDAClient

# Hook events
Register-ObjectEvent -InputObject $client -EventName ItemChanged -Action { Write-Host $EventArgs }

# Subscribe
$client.SubscribeItem("", "OPCLabs.KitServer.2", "Demo.Single", 1000)

# Wait for 1 minute
$stopwatch =  [System.Diagnostics.Stopwatch]::StartNew() 
while ($stopwatch.Elapsed.TotalSeconds -lt 60) {    
    Start-Sleep -Seconds 1
}

 

 

Useful links: PowerShell Examples / PowerShell in Knowledge Base / PowerShell examples GitHub repository

 

 

 

 

 

Footnote & required disclosure: QuickOPC (including its Options) is a software development kit (SDK) for development of OPC clients and subscribers. Installing QuickOPC or its Options does not change system settings.