我的目标是从 PowerShell 调用 Google Admin SDK Directory 更新。我从一个工作的 .NET 应用程序开始,并将其转换为一个类库(我测试过——它可以工作)。我将其加载到模块清单中
RequiredAssemblies = @('My.GoogleAdminSDK.Directory.dll')
我的 Get-GoogleSDKUser 函数 New-Objects 我的类并在其上调用 GetUser。我得到:
Exception calling "GetUser" with "1" argument(s): "The type initializer for
'DotNetOpenAuth.Logger' threw an exception."
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\My.googleadminsdkdirectory.admin\My.GoogleA
dminSDKDirectory.Admin.psm1:56 char:9
+ $service.GetUser($googleUserName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TypeInitializationException
我试过为 log4net 添加一个配置文件,它产生了一个空的日志文件,但这没有帮助。
注意:为了做到这一点,我必须添加
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.12.0" newVersion="1.2.12.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
到 powershell_ise.exe.config 以防止错误:
“无法加载文件或程序集 'System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。系统找不到指定的文件。”
这远非理想。
编辑
PowerShell-calling-my-DLL 版本的 InnerException 是:
无法加载文件或程序集 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' 或其依赖项之一。该系统找不到指定的文件。
花了一些时间才找到 innerException。我有一些雪貂,并找到了使用 Fuslogvw 的建议,所以我尝试了。我关闭并重新打开 PowerShell,启动 Fusion Log Viewer(这是我第一次使用它)并找到三个 log4net 条目。说明如下:
- log4net
- log4net,版本=1.2.12.0,文化=中性,PublicKeyToken=669e0ddf0bb1aa2a
- log4net,版本=1.2.10.0,文化=中性,PublicKeyToken=1b44e1d426115821
当我打开它们时,前两个有“手术成功”。作为文本的第二行。第三个有'操作失败。绑定结果:hr = 0x80070002。该系统找不到指定的文件。'
当我深入研究该日志时,我发现:
LOG: Assembly download was successful. Attempting setup of file: C:\windows\system32\windowspowershell\v1.0\Modules\JLP.GoogleAdminSDKDirectory.Admin\log4net.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: log4net, Version=1.2.12.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a
WRN: Comparing the assembly name resulted in the mismatch: Build Number
ERR: The assembly reference did not match the assembly definition found.
我尝试再次从 Visual Studio 运行它。fuslogvw 中也有三个条目,第三个有相同的错误。但是 VS 输出中没有任何内容表明存在问题。