[英]Loading multiple assemblies in PowerShell with -AssemblyName
我想加載 4 個程序集,目前我是這樣加載它們的:
Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.ManagedDTS\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.ManagedDTS.dll';
Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.DTSPipelineWrap\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.DTSPipelineWrap.dll';
Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.PipelineHost\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.PipelineHost.dll';
Add-Type -path 'C:\Windows\Microsoft.NET\assembly\GAC_64\Microsoft.SqlServer.DTSRuntimeWrap\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.DTSRuntimeWrap.dll'
當我嘗試像這樣加載它們時:
Add-Type -AssemblyName Microsoft.SqlServer.ManagedDTS, Microsoft.SqlServer.PipelineHost, Microsoft.SqlServer.DTSRuntimeWrap, Microsoft.SqlServer.DTSPipelineWrap
我收到以下錯誤:
Add-Type : Cannot add type. The assembly 'Microsoft.SqlServer.ManagedDTS' could not be found.
At line:1 char:1
+ Add-Type -AssemblyName Microsoft.SqlServer.ManagedDTS, Microsoft.SqlS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.SqlServer.ManagedDTS:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand
如果我只嘗試加載一個程序集:
Add-Type -AssemblyName Microsoft.SqlServer.ManagedDTS
現在有兩條錯誤信息:
Add-Type : Cannot add type. The assembly 'Microsoft.SqlServer.ManagedDTS' could not be found.
At line:1 char:1
+ Add-Type -AssemblyName Microsoft.SqlServer.ManagedDTS
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.SqlServer.ManagedDTS:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. One or more required assemblies are missing.
At line:1 char:1
+ Add-Type -AssemblyName Microsoft.SqlServer.ManagedDTS
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
也許這會有所幫助。
If ($host.Name -eq 'ConsoleHost' -or
$host.Name -eq 'Visual Studio Code Host') {
try{ <#+------------------------------------------------+
| Check that the proper assemblies are loaded |
| Required for PS Console and Visual Code, while |
| only Systems.Windows.Forms needed for PSISE! |
+------------------------------------------------+
#>
$ATArgs = @{AssemblyName = "PresentationCore",
"PresentationFramework",
"WindowsBase"
ErrorAction = 'Stop'}
Add-Type @ATArgs
}
catch {
Write-Host $("Failed to load Windows Presentation Framework" +
" and/or other assemblies required for this program!")
Exit
}
} #End If ($host…
我在使用基於 XAML 的菜單的所有腳本中都包含此代碼,以確保當我在 CLI Powershell 中運行時加載所有必要的程序集,通常在使用 VSCode 或 PS-ISE 時不需要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.