C# Powershell Cmdlet缺少程序集Google Api
仅在尝试创建Powershell Cmdlet时出现问题。我可以在控制台应用程序中的流程记录中执行相同的代码。我的想法是App.config没有重定向程序集,但是没有成功地使其工作。我使用nuget包来引用Google api dotnet客户端 此处的示例代码C# Powershell Cmdlet缺少程序集Google Api,c#,powershell,google-api-dotnet-client,C#,Powershell,Google Api Dotnet Client,仅在尝试创建Powershell Cmdlet时出现问题。我可以在控制台应用程序中的流程记录中执行相同的代码。我的想法是App.config没有重定向程序集,但是没有成功地使其工作。我使用nuget包来引用Google api dotnet客户端 此处的示例代码 错误 PsTest.cs using System.ComponentModel; using System.Collections.Generic; using Google.Apis.Admin.Directory.direct
错误
PsTest.cs
using System.ComponentModel;
using System.Collections.Generic;
using Google.Apis.Admin.Directory.directory_v1.Data;
using System.Management.Automation;
using System;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Services;
using Google.Apis.Util;
using System.Security.Cryptography.X509Certificates;
namespace PSTest
{
[RunInstaller(true)]
public class PsTest : PSSnapIn
{
/// <summary>
/// Specify a description of the PowerShell snap-in.
/// </summary>
public override string Description
{
get
{
return "This is a PowerShell snap-in that includes the Google Apps Lib";
}
}
/// <summary>
/// Specify the localization resource information for the description.
/// Use the format: resourceBaseName,Description.
/// </summary>
public override string DescriptionResource
{
get
{
return "GoogleAppsPSSnapIn,This is a PowerShell snap-in that includes the Google Apps Lib";
}
}
/// <summary>
/// Specify the name of the PowerShell snap-in.
/// </summary>
public override string Name
{
get
{
return "GoogleApps";
}
}
/// <summary>
/// Specify the vendor for the PowerShell snap-in.
/// </summary>
public override string Vendor
{
get
{
return "somecompany";
}
}
/// <summary>
/// Specify the localization resource information for the vendor.
/// Use the format: resourceBaseName,VendorName.
/// </summary>
public override string VendorResource
{
get
{
return "GoogleAppsPSSnapIn,somecompany";
}
}
}
[Cmdlet("Get", "GAppsUsers")]
public class GetGAppsUsers : PSCmdlet
{
protected override void ProcessRecord()
{
const string _serviceAccountCertPath = @"C:\cert.p12";
const string _serviceAccountEmail = @"id@developer.gserviceaccount.com";
const string _serviceAccountUser = @"someone@somewhere.com";
var certificate = new X509Certificate2(
_serviceAccountCertPath, "notasecret", X509KeyStorageFlags.Exportable);
var scopes = DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() + @" " +
DirectoryService.Scopes.AdminDirectoryGroup.GetStringValue() + @" " +
DirectoryService.Scopes.AdminDirectoryOrgunit.GetStringValue() + @" " +
DirectoryService.Scopes.AdminDirectoryDeviceChromeos.GetStringValue() + @" " +
DirectoryService.Scopes.AdminDirectoryDeviceMobile.GetStringValue() + @" " +
DirectoryService.Scopes.AdminDirectoryDeviceMobileAction.GetStringValue();
var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
{
ServiceAccountId = _serviceAccountEmail,
Scope = @scopes
};
if (_serviceAccountUser != string.Empty)
{
provider.ServiceAccountUser = _serviceAccountUser;
}
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
var _directoryService = new DirectoryService((new BaseClientService.Initializer() { Authenticator = auth }));
var service = _directoryService.Users.List();
service.Customer = "my_customer";
service.OrderBy = UsersResource.ListRequest.OrderByEnum.FamilyName;
service.SortOrder = UsersResource.ListRequest.SortOrderEnum.ASCENDING;
service.MaxResults = 500;
var allUsers = new List<User>();
var users = service.Execute();
Int64 count = 0;
if (String.IsNullOrEmpty(users.NextPageToken))
{
service.PageToken = users.NextPageToken;
allUsers.AddRange(users.UsersValue);
count = (count + users.UsersValue.Count);
Console.Write(count + "...");
}
while (!String.IsNullOrEmpty(users.NextPageToken))
{
service.PageToken = users.NextPageToken;
allUsers.AddRange(users.UsersValue);
count = (count + users.UsersValue.Count);
Console.Write(count + "...");
users = service.Execute();
if (String.IsNullOrEmpty(users.NextPageToken))
{
service.PageToken = users.NextPageToken;
allUsers.AddRange(users.UsersValue);
count = (count + users.UsersValue.Count);
Console.Write(count + "...");
}
}
WriteObject(allUsers);
}
}
}
将System.Net.Http.Primitives版本2.1.10和1.5添加到GAC
Get-GAppsUsers : Method not found: 'Void System.Net.Http.HttpClientHandler.set_
AutomaticDecompression(System.Net.DecompressionMethods)'.
解决方案
- System.Net.Http 2.1.10.0
- System.Net.Http.Primitives 2.1.10.0
- log4net 1.2.10.0
请在此处发布代码,以便更多人可以帮助您吗?在post中添加了示例代码。请使用Nuget安装软件包Microsoft.Net.Http。请使用fuslogvw,如本so答案所示,找出问题所在@KeithHill Microsoft.Net.Http 2.1.10是通过Nuget per Google Api DotNet客户端安装的。我也在一条非常类似的船上,但有一个问题——这是否必须在每台使用cmdlet的计算机上进行?如果是这样的话,这可以通过编程实现吗?是的,必须对每个人都这样做,但我相信这个问题可以以更好的方式解决。还没有时间在这方面做更多的工作。谢谢。我不会假装理解到底发生了什么(老实说,这是我第一次听说GAC),但它现在起作用了。至少是这样。使用GAC的唯一原因是,默认情况下,“Powershell.exe”进程查找程序集的唯一其他位置是在其安装目录(c:\windows\system32\windowspowershell\v1.0)中。如果要避免GAC和机器配置MOD,可以尝试在管理单元和OnIMPort钩子中实现IModuleAssemblyInitializer,即当前appdomain的AssemblyResolve事件。当appdomain尝试加载在通常位置(app base dir,GAC)找不到的程序集时,将触发该命令。然后可以提供这些程序集的路径(在管理单元目录中)。
<?xml version="1.0"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0"/>
</dependentAssembly>
</assemblyBinding>
<loadfromRemoteSources enabled="true"/>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
*** Assembly Binder Log Entry (8/31/2013 @ 8:02:43 PM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = NinjaWinMontage\mosheldon
LOG: DisplayName = System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/Windows/System32/WindowsPowerShell/v1.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = powershell.exe
Calling assembly : Google.Apis, Version=1.5.0.28972, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\mosheldon\Desktop\PSTest\PSTest\bin\Debug\System.Net.Http.Primitives.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Net.Http.Primitives, Version=2.1.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: All probing URLs attempted and failed.
*** Assembly Binder Log Entry (8/31/2013 @ 8:02:43 PM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = NinjaWinMontage\mosheldon
LOG: DisplayName = System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/Windows/System32/WindowsPowerShell/v1.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = powershell.exe
Calling assembly : Google.Apis, Version=1.5.0.28972, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\mosheldon\Desktop\PSTest\PSTest\bin\Debug\System.Net.Http.Primitives.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Net.Http.Primitives, Version=2.1.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: All probing URLs attempted and failed.
*** Assembly Binder Log Entry (8/31/2013 @ 8:08:35 PM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = NinjaWinMontage\mosheldon
LOG: DisplayName = System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/Windows/System32/WindowsPowerShell/v1.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = powershell.exe
Calling assembly : Google.Apis, Version=1.5.0.28972, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\mosheldon\Desktop\PSTest\PSTest\bin\Debug\System.Net.Http.Primitives.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Net.Http.Primitives, Version=2.1.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: All probing URLs attempted and failed.
*** Assembly Binder Log Entry (8/31/2013 @ 8:08:35 PM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = NinjaWinMontage\mosheldon
LOG: DisplayName = System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/Windows/System32/WindowsPowerShell/v1.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = powershell.exe
Calling assembly : Google.Apis, Version=1.5.0.28972, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\mosheldon\Desktop\PSTest\PSTest\bin\Debug\System.Net.Http.Primitives.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Net.Http.Primitives, Version=2.1.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives.EXE.
LOG: Attempting download of new URL file:///C:/Users/mosheldon/Desktop/PSTest/PSTest/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
LOG: All probing URLs attempted and failed.
Get-GAppsUsers : Method not found: 'Void System.Net.Http.HttpClientHandler.set_
AutomaticDecompression(System.Net.DecompressionMethods)'.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>