Connectivity Software User's Guide and Reference
Programming in PowerShell
QuickOPC > Fundamentals > Development Fundamentals > Programming in PowerShell
In This Topic

Principles of Operation

Connectivity Software development products are primarily written in Microsoft .NET, and PowerShell is natively a .NET development language and enviroment. Therefore, in principle, .NET-related descriptions in this documentation set apply to PowerShell as well, and PowerShell programs are created in the same way as C# or VB.NET programs. There are, however, some slight differences to be aware of.

Referencing the Assemblies

In PowerShell, use the Add-Type cmdlet to make Connectivity Software classes available in your PowerShell session. You need to specify a full path to Connectivity Software assemblies, and the assembly name that you want to use. 

QuickOPC Example

# This example shows how to read value of a single node, and display it.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
# OPC client and subscriber examples in PowerShell on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-PowerShell .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.

#requires -Version 5.1
using namespace OpcLabs.EasyOpc.UA
using namespace OpcLabs.EasyOpc.UA.OperationModel

# The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows .
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUA.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUAComponents.dll"

[UAEndpointDescriptor]$endpointDescriptor =
    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
# or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
# or "https://opcua.demo-this.com:51212/UA/SampleServer/"

# Instantiate the client object.
$client = New-Object EasyUAClient

Write-Host "Obtaining value of a node..."
try {
    $value = $client.ReadValue($endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853")
}
catch [UAException] {
    Write-Host "*** Failure: $($PSItem.Exception.GetBaseException().Message)"
    return
}

# Display results
Write-Host "value: $($value)"

OPC Wizard Example

# This example shows how to create a data variable and implement reading its value using a function.
# You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client, server and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.

#requires -Version 5.1
using namespace OpcLabs.EasyOpc.UA
using namespace OpcLabs.EasyOpc.UA.NodeSpace
using namespace OpcLabs.PowerShellManagement

# The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows .
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.ServerOpcUAComponents.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.PowerShellManagement.dll"

# Instantiate the server object.
# By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
$server = New-Object EasyUAServer

# Create a data variable, defining its read values by a function.
# The type of the data variable (Int32, in this case) is inferred from the type returned by the function.
$random = New-Object System.Random
$server.Add([UADataVariableExtension]::ReadValueFunction((New-Object UADataVariable("ReadThisVariable")), 
    [RunspacedDelegateFactory]::NewRunspacedDelegate([System.Func[int]] { $random.Next() })))

# Start the server.
Write-Host "The server is starting..."
$server.Start()

Write-Host "The server is started."
Write-Host

# Let the user decide when to stop.
Write-Host "Press Enter to stop the server..."
Read-Host

# Stop the server.
Write-Host "The server is stopping..."
$server.Stop()

Write-Host "The server is stopped."

Component Callbacks 

PowerShell has some special requirements when its code is called (as a callback) from the components you use in your PowerShell script. If the callback is made from a thread that is different from the script thread, an error like "There is no Runspace available to run scripts in this thread. …" can occur internally. In many cases, the error will be handled internally, and to you, it will appear as if the callback had not been called at all.

In order to overcome this issue, you can use the function [RunspacedDelegateFactory]::NewRunspacedDelegate to wrap your PowerShell callback. This function is contained in the OpcLabs.PowerShellManagement assembly that you references using the Add-Type cmdlet. This technique is illustrated throughout our example wherever it is necessary, so you can learn the precise syntax from there.

 

See Also

Introduction

Development

Examples - Client OPC Unified Architecture