我正在尝试使用 PowerShell 来自动化创建基于种子(想想 EDMX 文件或 DbContext)配置的 n 层解决方案的过程。我希望能够打开一个框架解决方案,获取活动实例,并使用自动生成的代码填充项目文件。
我正在尝试将此处提供的示例转码为 powershell,但是,我遇到了错误。
这是我正在测试的 PowerShell 代码:
首先,我执行一个小函数来引用 DTE 程序集。
$libs = "envdte.dll", "envdte80.dll", "envdte90.dll", "envdte100.dll"
function LoadDTELibs {
param(
$path = "\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies"
)
Process {
$libs |
ForEach {
$dll = Join-Path "$env:ProgramFiles\$path" $_
if(-not (Test-Path $dll)) {
$dll = Join-Path "${env:ProgramFiles(x86)}\$path" $_
}
Add-Type -Path $dll -PassThru | Where {$_.IsPublic -and $_.BaseType} | Sort Name
}
}
}
LoadDTELibs
然后,我尝试创建一个对象来引用调用的结果[System.Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.11.0")
PS> $dte = New-Object -ComObject EnvDTE80.DTE2
New-Object : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000} failed due to the following error: 80040154
Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
At line:1 char:8
+ $dte = New-Object -ComObject EnvDTE80.DTE2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
或者:
PS> $dte = New-Object EnvDTE80.DTE2
New-Object : Constructor not found. Cannot find an appropriate constructor for type EnvDTE80.DTE2.
At line:1 char:8
+ $dte = New-Object EnvDTE80.DTE2
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
最后,这也不起作用:
PS> [EnvDTE80.DTE2]$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.11.0")
Cannot convert the "System.__ComObject" value of type "System.__ComObject#{04a72314-32e9-48e2-9b87-a63603454f3e}" to type "EnvDTE80.DTE2".
At line:1 char:1
+ [EnvDTE80.DTE2]$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
所以,我的问题是,你如何使用 PowerShell 中的 DTE?更具体地说,如何将调用 GetActiveObject 的结果转换为类型 EnvDTE.DTE2?