从[Reflection.Assembly] :: Load()加载的DLL导出Powershell CMDlet

问题描述 投票:1回答:2

我有一个环境,文件无法写入磁盘,所以我通常从GAC的内存中加载DLL。

虽然加载不是问题,但是暴露DLL命令行开关是。大会加载:

[appdomain]::currentdomain.getassemblies() | sort -property fullname | format-table fullname

FullName
--------
AudioDeviceCmdlets, Version=3.0.0.4, Culture=neutral, PublicKeyToken=null

在*

CodeBase            : file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
FullName            : AudioDeviceCmdlets, Version=3.0.0.4, Culture=neutral, PublicKeyToken=null
EntryPoint          :
DefinedTypes        : {CoreAudioApi.AudioEndpointVolume, CoreAudioApi.AudioEndpointVolumeCallback, CoreAudioApi.AudioEndpointVolumeChannel, CoreAudioApi.AudioEndpointVolumeChannels...}
Evidence            : {<System.Security.Policy.Url version="1">
                      <Url>file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll</Url>
                      </System.Security.Policy.Url>
                      , <System.Security.Policy.Zone version="1">
                      <Zone>MyComputer</Zone>
                      </System.Security.Policy.Zone>
                      }
PermissionSet       : {}
SecurityRuleSet     : Level2
ManifestModule      : AudioDeviceCmdlets.dll
ReflectionOnly      : False
Location            :
ImageRuntimeVersion : v4.0.30319
GlobalAssemblyCache : False
HostContext         : 0
IsDynamic           : False
EscapedCodeBase     : file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
ExportedTypes       : {CoreAudioApi.AudioEndpointVolume, CoreAudioApi.AudioEndpointVolumeChannel, CoreAudioApi.AudioEndpointVolumeChannels, CoreAudioApi.AudioEndpointVolumeNotificationDelegate...}
IsFullyTrusted      : True
CustomAttributes    : {[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)], [System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)],
                      [System.Diagnostics.DebuggableAttribute((System.Diagnostics.DebuggableAttribute+DebuggingModes)2)], [System.Reflection.AssemblyTitleAttribute("AudioDeviceCmdlets")]...}
Modules             : {<unknown>}

..但CMDLets不会导出..当然我肯定会遗漏一些东西。

相同的DLL,以“常规”方式加载:

load-module AudioDeviceCmdlets.dll

正确导出几个CmdLets,如Get-AudioDevice和Set-AudioDevice。

有什么提示我缺少哪些步骤来暴露dll中的Cmdlet?

powershell dll .net-assembly gac cmdlet
2个回答
1
投票

虽然看起来很粗鲁,(我仍然愿意接受更好的答案!)这有效:

1)选择可用程序集的第一个实例(因为可能有多个,或者更具体地找到要加载的程序集):

$Assembly=([System.AppDomain]::CurrentDomain.GetAssemblies()|? FullName -Match "AudioDeviceCmdlets")[0]

2)使用Import-Module导入装配,但不是指定要装入的dll,而是直接引用装配。如果没有清单,它将默认导出所有变量及其中的所有函数

Import-Module -Assembly $Assembly

瞧,现在的Commandlets正确导出了! :)


1
投票

使用Import-Module -Assembly

$assembly = [Reflection.Assembly]::Load($UncompressedFileBytes)    
Import-Module -Assembly $assembly

或者,创建一个加载程序集的模块清单,然后导入:

New-ModuleManifest .\AudioDeviceCmdlets.psd1 -RequiredAssemblies AudioDeviceCmdlets
Import-Module .\AudioDeviceCmdlets.psd1
© www.soinside.com 2019 - 2024. All rights reserved.