Samples | Visual Studio thread. However, PowerShell 7 runs by default in the Multi-Threaded Apartment (MTA). The STA requirement must therefore be explicitly met by creating a separate STA runspace. This article highlights the most important pitfall and how to avoid it. Requirements TwinCAT XAE and PowerShell 7.x must be installed. Setting the STA mode COM objects in the Automation Interface require STA (Single-Threaded Apartment). The STA model guarantees that all calls to a COM object are serialized and run on exactly one thread. That is exactly what the Automation Interface expects.
If, on the other hand, the object is accessed from an MTA thread, errors such as RPC_E_SERVERCALL_RETRYLATER or unexpected marshalling issues may occur. Since PowerShell 7 starts in MTA mode by default, a separate STA runspace is created. A runspace is created via RunspaceFactory , with its ApartmentState explicitly set to "STA" . The setting ThreadOptions = "ReuseThread" ensures that all subsequent calls use the same thread. This is absolutely necessary because an STA-COM object may only be accessed by the thread that created it. The actual COM code is then executed within this STA runspace via AddScript({ ... }).Invoke() :
$ps = [PowerShell]::Create() $rs = [RunSpaceFactory]::CreateRunspace() $rs.ApartmentState = "STA" $rs.ThreadOptions = "ReuseThread" $rs.Open() $ps.Runspace = $rs $ps.AddScript({ $dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("TcXaeShell.DTE.17.0") $project = $dte.Solution.Projects.Item(1) $project.Object # ITcSysManager }).Invoke() 6.1.4 Which DTE ProgID should be used for the respective TwinCAT/VS version? | Which DTE ProgID should be used for the respective TwinCAT/VS ver ... The ProgID for New-Object -ComObject or GetActiveObject depends on whether TcXaeShell or Visual Studio is used as the host and which version is installed.
A ProgID is a Programmatic Identifier and the human-readable name under which a COM server is registered in the registry and in the Running Object Table (ROT). Internally, it is mapped to a unique CLSID. If the wrong ProgID is used, the connection will fail because either no matching COM server is registered or a different IDE than the expected one is being addressed. Therefore, the installed host variant and its version should be clearly known before establishing a connection. Overview of ProgIDs TE1000 188 Version: 1.7.0 ProgID Host Basic VS version TcXaeShell.DTE. 17.0 TwinCAT XAE Shell (based on VS 2022) VS 2022 TcXaeShell.DTE.
15.0 TwinCAT XAE Shell (based on VS 2017) VS 2017 VisualStudio.DT E.18.0 Visual Studio 2026 with the TwinCAT plugin VS 2026 VisualStudio.DT E.17.0 Visual Studio 2022 with the TwinCAT plugin VS 2022 VisualStudio.DT E.16.0 Visual Studio 2019 with the TwinCAT plugin VS 2019 VisualStudio.DT E.15.0 Visual Studio 2017 with the TwinCAT plugin VS 2017