【问题标题】:Using WUA remotely using C#使用 C# 远程使用 WUA
【发布时间】:2011-11-30 03:59:07
【问题描述】:

我正在尝试连接到远程计算机并确定是否有任何要使用 C# 和 WUApiLib 安装的 Windows 更新。看起来很简单,但是我不知道如何实际连接到远程计算机。

http://msdn.microsoft.com/en-us/library/aa387288(v=VS.85).aspx 表示可以远程使用,除了一些我不关心的例外,但似乎没有关于如何实际连接的文档。我尝试传入一个计算机名称,但这并不奏效(传递一个包含乱码“成功”的名称与不传递任何内容相同,并导致相同的计数,因此我假设该字符串未使用。)

var updateSession = new UpdateSession(dependencies.ComputerName);
var searcher = updateSession.CreateUpdateSearcher();
var results = searcher.Search("IsInstalled=0 and Type='Software'");

有谁知道谁通过 C# 远程使用 WUA?

【问题讨论】:

  • 我也想知道如何确定更新的大小。

标签: c# c#-4.0 windows-update


【解决方案1】:

几年前我也在寻找同样的东西并写了一个小脚本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WUApiLib;//this is required to use the Interfaces given by microsoft. 
namespace MSHWindowsUpdateAgent
{
    class Program
    {
        static void Main(string[] args)
        {
            UpdatesAvailable();
            if (NeedsUpdate())
            {
                EnableUpdateServices();//enables everything windows need in order to make an update
                InstallUpdates(DownloadUpdates());
            }
            else
            {
                Console.WriteLine("There are no updates for your computer at this time.");
            }
            Console.WriteLine("Press any key to finalize the process");
            Console.Read();
        }
        //this is my first try.. I can see the need for abstract classes here...
        //but at least it gives most people a good starting point.
        public static  void InstalledUpdates()
        {
            UpdateSession UpdateSession = new UpdateSession();
            IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
            UpdateSearchResult.Online = true;//checks for updates online
            ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=1 AND IsHidden=0");
            //for the above search criteria refer to 
            //http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
            //Check the remakrs section
            Console.WriteLine("The following updates are available");
            foreach (IUpdate x in SearchResults.Updates)
            {
                Console.WriteLine(x.Title);
            }
        }
        public static void UpdatesAvailable()
        {
            UpdateSession UpdateSession = new UpdateSession();
            IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
            UpdateSearchResult.Online = true;//checks for updates online
            ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=0 AND IsPresent=0");
            //for the above search criteria refer to 
            //http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
            //Check the remakrs section

            foreach (IUpdate x in SearchResults.Updates)
            {
                Console.WriteLine(x.Title);
            }
        }
        public static bool NeedsUpdate()
        {
            UpdateSession UpdateSession = new UpdateSession();
            IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
            UpdateSearchResult.Online = true;//checks for updates online
            ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=0 AND IsPresent=0");
            //for the above search criteria refer to 
            //http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
            //Check the remakrs section
            if (SearchResults.Updates.Count > 0)
                return true;
            else return false;
        }
        public static UpdateCollection DownloadUpdates()
        {
            UpdateSession UpdateSession = new UpdateSession();
            IUpdateSearcher SearchUpdates = UpdateSession.CreateUpdateSearcher();
            ISearchResult UpdateSearchResult = SearchUpdates.Search("IsInstalled=0 and IsPresent=0");
            UpdateCollection UpdateCollection = new UpdateCollection();
            //Accept Eula code for each update
            for (int i = 0; i < UpdateSearchResult.Updates.Count; i++)
            {
                IUpdate Updates = UpdateSearchResult.Updates[i];
                if (Updates.EulaAccepted == false)
                {
                    Updates.AcceptEula();
                }
                UpdateCollection.Add(Updates);
            }
            //Accept Eula ends here
            //if it is zero i am not sure if it will trow an exception -- I havent tested it.
            if (UpdateSearchResult.Updates.Count > 0)
            {
                UpdateCollection DownloadCollection = new UpdateCollection();
                UpdateDownloader Downloader = UpdateSession.CreateUpdateDownloader();

                for (int i = 0; i < UpdateCollection.Count; i++)
                {
                    DownloadCollection.Add(UpdateCollection[i]);
                }

                Downloader.Updates = DownloadCollection;
                Console.WriteLine("Downloading Updates... This may take several minutes.");


                IDownloadResult DownloadResult = Downloader.Download();
                UpdateCollection InstallCollection = new UpdateCollection();
                for (int i = 0; i < UpdateCollection.Count; i++)
                {
                    if (DownloadCollection[i].IsDownloaded)
                    {
                        InstallCollection.Add(DownloadCollection[i]);
                    }
                }
                Console.WriteLine("Download Finished");
                return InstallCollection;
            }
            else
                return UpdateCollection;
        }
        public static void InstallUpdates(UpdateCollection DownloadedUpdates)
        {
            Console.WriteLine("Installing updates now...");
            UpdateSession UpdateSession = new UpdateSession();
            UpdateInstaller InstallAgent = UpdateSession.CreateUpdateInstaller() as UpdateInstaller;
            InstallAgent.Updates = DownloadedUpdates;

            //Starts a synchronous installation of the updates.
            // http://msdn.microsoft.com/en-us/library/windows/desktop/aa386491(v=VS.85).aspx#methods
            if (DownloadedUpdates.Count > 0)
            {
                IInstallationResult InstallResult = InstallAgent.Install();
                if (InstallResult.ResultCode == OperationResultCode.orcSucceeded)
                {
                    Console.WriteLine("Updates installed succesfully");
                    if (InstallResult.RebootRequired == true)
                    {
                        Console.WriteLine("Reboot is required for one of more updates.");
                    }
                }
                else
                {
                    Console.WriteLine("Updates failed to install do it manually");
                }
            }
            else
            {
                Console.WriteLine("The computer that this script was executed is up to date");
            }

        }
        public static void EnableUpdateServices()
        {
            IAutomaticUpdates updates = new AutomaticUpdates();
            if (!updates.ServiceEnabled)
            {
                Console.WriteLine("Not all updates services where enabled. Enabling Now" + updates.ServiceEnabled);
                updates.EnableService();
                Console.WriteLine("Service enable success");
            }


        }

    }
}

【讨论】:

  • 这不是问题的答案;只是一个“我也有这个问题”的投票,附有一堆代码。
  • 我做了你为什么要问?
  • 这个“附加代码”有效地回答了这个问题。
  • 是的......代码 100% 完全正常工作,它完全符合他的需要。我只是说不是生产就绪代码。
【解决方案2】:
        Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", "SYDEMMTA001");
        UpdateSession session = (UpdateSession)Activator.CreateInstance(t);
        IUpdateSearcher updateSearcher = session.CreateUpdateSearcher();
        //int count = updateSearcher.GetTotalHistoryCount();
        //IUpdateHistoryEntryCollection history = updateSearcher.QueryHistory(0, count);
        //for (int i = 0; i < count; ++i)
        //{
        //    Console.WriteLine(string.Format("Title: {0}\tSupportURL: {1}\tDate: {2}\tResult Code: {3}\tDescription: {4}\r\n", history[i].Title, history[i].SupportUrl, history[i].Date, history[i].ResultCode, history[i].Description));
        //}
        //Console.WriteLine("Total up: {0}", count);
        ISearchResult sResult = updateSearcher.Search("IsInstalled=0 AND IsPresent=0");
        foreach (IUpdate x in sResult.Updates)
        {
            Console.WriteLine(x.Title);
        }

【讨论】:

    【解决方案3】:

    试试这个:

    Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", HOSTNAME);
    UpdateSession uSession = (UpdateSession)Activator.CreateInstance(t);
    
    IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
    uSearcher.ServerSelection = ServerSelection.ssWindowsUpdate;
    uSearcher.IncludePotentiallySupersededUpdates = false;
    uSearcher.Online = false;
    
    ISearchResult sResult = uSearcher.Search(
        "IsInstalled=0 And IsHidden=0 And Type='Software'");
    

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2013-09-18
      • 2014-07-25
      • 2023-03-16
      • 2010-12-27
      相关资源
      最近更新 更多