I am trying to convert the following C# code to PowerShell (I have left out the parts of the code that I do not think are relevant):
C#
System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0", true);
object obj = System.Activator.CreateInstance(t, true);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)obj;
dte.MainWindow.Activate();
dynamic solution = dte.Solution;
solution.Create(solution_folder_path, solution_name);
solution.SaveAs(solution_file_path);
dynamic project = solution.AddFromTemplate(template_path, project_folder_path, project_name);
TCatSysManagerLib.ITcSysManager system_manager = project.Object;
PowerShell
[System.Reflection.Assembly]::LoadFrom("C:\Windows\assembly\GAC_MSIL\TCatSysManagerLib\2.1.0.0__180016cd49e5e8c3\TCatSysManagerLib.dll");
$dte = New-Object -ComObject VisualStudio.DTE.10.0
$dte.SuppressUI = $false
$dte.MainWindow | %{$_.GetType().InvokeMember("Visible", "SetProperty", $null, $_, $true)}
$solution = $dte.Solution
$project = $solution.AddFromTemplate($template_path, $project_folder_path, $project_name)
[TCatSysManagerLib.ITcSysManager] $system_manager = [TCatSysManagerLib.ITcSysManager] $project.Object
The problem that I run into is that the last line in PowerShell returns an error of the form:
Cannot convert the "System.__ComObject" value of type "System.__ComObject#{3b56a5ce-0c02-440b-8ced-f1f3e83f66ed}" to type "TCatSysManagerLib.ITcSysManager".
Added details:
I could just leave out the [TCatSysManagerLib.ITcSysManager]
type in the PowerShell code. I run into the same problem however in converting this C# code later on:
TCatSysManagerLib.ITcSmTreeItem gvl = system_manager.LookupTreeItem("TIPC^PLC1^PLC1 Project^GVLs^GVL");
TCatSysManagerLib.ITcPlcDeclaration gvl_declaration = (TCatSysManagerLib.ITcPlcDeclaration)gvl;
string gvl_declaration_text = System.IO.File.ReadAllText(gvl_declaration_text_file_path);
gvl_declaration.DeclarationText = gvl_declaration_text;
In this case I have to make the cast to the TCatSysManagerLib.ITcPlcDeclaration
type in order to gain access to the DeclarationText
property of gvl_declaration
.
The C# code is modified from: http://infosys.beckhoff.com/english.php?content=../content/1033/tc3_automationinterface/108086391299624331.html&id=
I am new to both C# and PowerShell and have modified these from examples. Any advice would be appreciated.