C# 在AdminProxy类中找不到GetUpdateServer
这是我试图运行的powershell comand。但有一个例外是——C# 在AdminProxy类中找不到GetUpdateServer,c#,windows,powershell,windows-update,C#,Windows,Powershell,Windows Update,这是我试图运行的powershell comand。但有一个例外是—— $servername='DC1' [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | Out-Null $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($servername,$False)
$servername='DC1'
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | Out-Null
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($servername,$False)
当我尝试重新引用Microsoft.UpdateServices.Administration.dll并尝试使用GetUpdateServer方法时,在AdminProxy类中找不到该方法。这是代码行-
Exception calling "GetUpdateServer" with "2" argument(s): "The type initializer for
'Microsoft.UpdateServices.Internal.Constants' threw an exception."
At line:3 char:1
+ $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TypeInitializationException
正如用户boeprox在评论中指出的,您需要以管理员身份运行PowerShell控制台。注意,必须安装WSUS管理控制台才能访问所需的程序集。您安装了吗?是的,我安装了Wsus管理控制台。您是否以管理员身份运行PowerShell控制台?我看到过导致此错误发生的UAC问题,当您右键单击PowerShell并选择“以管理员身份运行”时,这些问题已得到解决。。谢谢
using Microsoft.UpdateServices.Administration;
using Microsoft.UpdateServices;
using System.Runtime.InteropServices;
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[GuidAttribute("6391AFBB-45A8-4107-A154-F27DB8F03049")]
public sealed class AdminProxy
{
}
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IUpdateServer obj;
obj = AdminProxy.GetUpdateServer();
}
}