Samples | System PowerShell 5 sample In PowerShell 5, establishing a connection to a running instance is straightforward because Windows PowerShell is based on the classic .NET Framework, where Marshal.GetActiveObject is available. There is no need to manually import OLE functions. The following command attaches to the running IDE, opens the specified solution, and accesses the first project. Since dte.Solution.Open() does not return a value, the project is then retrieved using Projects.Item(1) . Its Object property is the ITcSysManager interface.
$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("TcXaeShell.DTE.17.0") $dte.Solution.Open("C:\Projects\MyProject\MyProject.sln") $project = $dte.Solution.Projects.Item(1) $sysManager = $project.Object PowerShell and the TwinCAT instance must have the same “Elevation Level”. If TwinCAT is running as an administrator but PowerShell is not, or vice versa, the ROT entries are located in separate sessions and are not visible. PowerShell 7 sample PowerShell 7 is based on .NET 5+. Marshal.GetActiveObject is no longer included by default there. Therefore, you must add the required types yourself via Add-Type .
The following inline type definition encapsulates the same P/Invoke calls as the C# sample – namely, CLSIDFromProgID and GetActiveObject – in a class named ROT . After compiling the type definition, the static method [ROT]::GetActiveObject(...) is available. The rest of the process is the same as in PowerShell 5: Open the solution, load the project, and evaluate Object .
Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; public class ROT { [DllImport("ole32.dll")] private static extern int CLSIDFromProgID( [MarshalAs(UnmanagedType.LPWStr)] string lpszProgID, out Guid pclsid); [DllImport("oleaut32.dll")] private static extern int GetActiveObject( ref Guid rclsid, IntPtr pvReserved, [MarshalAs(UnmanagedType.IUnknown)] out object ppunk); public static object GetActiveObject(string progId) { Guid clsid; CLSIDFromProgID(progId, out clsid); object obj; int hr = GetActiveObject(ref clsid, IntPtr.Zero, out obj); if (hr != 0) Marshal.ThrowExceptionForHR(hr); return obj; } } "@ $dte =[System.Runtime.InteropServices.Marshal]::GetActiveObject("TcXaeShell.DTE.17.0") $dte.Solution.Open("C:\Projects\MyProject\MyProject.sln") $project = $dte.Solution.Projects.Item(1) $sysManager = $project.Object PowerShell and the TwinCAT instance must have the same “Elevation Level”.
If TwinCAT is running as an administrator but PowerShell is not, or vice versa, the ROT entries are located in separate sessions and are not visible. Attaching to a running VS instance via the Running Object Table If multiple VS instances are running and you are searching for a specific solution file, you can use the Running Object Table (ROT). The DteHelper class fully implements this approach in C#. Unlike a simple call to GetActiveObject , which returns only the first instance of a ProgID found, this approach iterates TE1000 204 Version: 1.7.0