如何将 .NET 程序集正确添加到 Powershell 会话?

ama*_*ion 30 powershell .net

我有一个 .NET 程序集(一个 dll),它是我们在这里使用的备份软件的 API。它包含一些我想在我的 Powershell 脚本中利用的属性和方法。但是,我遇到了很多问题,首先加载程序集,然后在加载程序集后使用任何类型。

完整的文件路径是:

C:\rnd\CloudBerry.Backup.API.dll
Run Code Online (Sandbox Code Playgroud)

在 Powershell 中,我使用:

$dllpath = "C:\rnd\CloudBerry.Backup.API.dll"
Add-Type -Path $dllpath
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Add-Type : Unable to load one or more of the requested types. Retrieve the
LoaderExceptions property for more information.
At line:1 char:9
+ Add-Type <<<<  -Path $dllpath
+ CategoryInfo          : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
+ FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeComma
ndAdd-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Run Code Online (Sandbox Code Playgroud)

在另一个 .NET 程序集DotNetZip上使用相同的 cmdlet ,它有在站点上使用相同功能的示例,对我来说也不起作用。

我最终发现我似乎能够使用反射加载程序集:

[System.Reflection.Assembly]::LoadFrom($dllpath)
Run Code Online (Sandbox Code Playgroud)

虽然我不明白 Load、LoadFrom 或 LoadFile 方法之间的区别,但最后一种方法似乎有效。

但是,我似乎仍然无法创建实例或使用对象。每次尝试时,我都会收到描述 Powershell 无法找到任何公共类型的错误。

我知道课程在那里:

$asm = [System.Reflection.Assembly]::LoadFrom($dllpath)
$cbbtypes = $asm.GetExportedTypes()
$cbbtypes | Get-Member -Static
Run Code Online (Sandbox Code Playgroud)

---- 摘录开始----

   TypeName: CloudBerryLab.Backup.API.BackupProvider

Name                MemberType Definition
----                ---------- ----------
PlanChanged         Event          System.EventHandler`1[CloudBerryLab.Backup.API.Utils.ChangedEventArgs] PlanChanged(Sy...
PlanRemoved         Event          System.EventHandler`1[CloudBerryLab.Backup.API.Utils.PlanRemoveEventArgs] PlanRemoved...
CalculateFolderSize Method     static long CalculateFolderSize()
Equals              Method     static bool Equals(System.Object objA, System.Object objB)
GetAccounts         Method     static CloudBerryLab.Backup.API.Account[],     CloudBerry.Backup.API, Version=1.0.0.1, Cu...
GetBackupPlans      Method     static CloudBerryLab.Backup.API.BackupPlan[], CloudBerry.Backup.API, Version=1.0.0.1,...
ReferenceEquals     Method     static bool ReferenceEquals(System.Object objA, System.Object objB)
SetProfilePath      Method     static System.Void SetProfilePath(string profilePath)
Run Code Online (Sandbox Code Playgroud)

----摘录结束----

尝试使用静态方法失败,不知道为什么!!!

[CloudBerryLab.Backup.API.BackupProvider]::GetAccounts()
Unable to find type [CloudBerryLab.Backup.API.BackupProvider]: make sure that the     assembly containing this type is load
ed.
At line:1 char:42
+ [CloudBerryLab.Backup.API.BackupProvider] <<<< ::GetAccounts()
    + CategoryInfo          : InvalidOperation:     (CloudBerryLab.Backup.API.BackupProvider:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound
Run Code Online (Sandbox Code Playgroud)

任何指导表示赞赏!

小智 19

你能不能Add-Type用 try catch包围它并打印 LoaderExceptions 属性,因为错误说明要这样做。它可能会提供带有更详细错误消息的异常。

try
{
    Add-Type -Path "C:\rnd\CloudBerry.Backup.API.dll"
}
catch
{
    $_.Exception.LoaderExceptions | %
    {
        Write-Error $_.Message
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 没有办法使用相对路径吗? (2认同)

小智 0

我使用以下设置在 powershell 中加载自定义 csharp 控件。它允许在 powershell 内自定义和使用控件。

这是博客链接

http://justcode.ca/wp/?p=435

这是带有源代码的代码项目链接

http://www.codeproject.com/Articles/311705/Custom-CSharp-Control-for-Powershell

  • 欢迎来到服务器故障!我们确实更喜欢答案包含内容而不是指向内容的指针。虽然这从理论上可以回答这个问题,但[最好](​​http://meta.stackexchange.com/q/8259)在此处包含答案的基本部分,并提供参考链接。 (3认同)