My aim is to call Google Admin SDK Directory updates from PowerShell. I started out with a working .NET app and converted it into a class library (which I tested - it works). I load that in the module manifest with
RequiredAssemblies = @('My.GoogleAdminSDK.Directory.dll')
My Get-GoogleSDKUser function New-Objects my class and calls GetUser on it. And I get:
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
I've tried adding a config file for log4net which has produced an empty log file but that didn't help.
Note: in order to get this far, I had to add
<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>
to powershell_ise.exe.config to prevent the error:
"Could not load file or assembly 'System.Net.Http.Primitives,
Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or
one of its dependencies. The system cannot find the file specified."
which is far from ideal.
EDIT
The InnerException for the PowerShell-calling-my-DLL version was:
Could not load file or assembly 'log4net, Version=1.2.10.0,
Culture=neutral, PublicKeyToken=1b44e1d426115821' or one of its
dependencies. The system cannot find the file specified.
It took a bit of digging to find the innerException. I had a bit of a ferret around and found suggestions to use Fuslogvw, so I tried that. I closed and re-opened PowerShell, started up Fusion Log Viewer (it's the first time I've used it) and found three entries for log4net. The descriptions are:
- log4net
- log4net, Version=1.2.12.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a
- log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821
When I open them up, the first two have 'The operation was successful.' as the second line of text. The third has 'The operation failed. Bind result: hr = 0x80070002. The system cannot find the file specified.'
When I dig into that log, I find:
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.
I tried running it from Visual Studio again. There were also three entries in fuslogvw and the third had the same error. But there's nothing in the VS output to indicate a problem.