查找远程系统中是否存在特定产品

发布于 2024-07-08 11:13:25 字数 1629 浏览 7 评论 0原文

尝试查找远程PC上是否安装了该产品,执行时间较长, 有什么方法可以让我们更快地实现这一目标吗?

//用法: //uninstall4("主机名", "产品名", "{AC9C1263-2BA8-4863-BE18-01232375CE42}", "10.0.0.69");

    public void uninstall4(string targetServer, string product,string guid , string version)
    {
        //Connect to Server
        System.Management.ConnectionOptions connoptions = new System.Management.ConnectionOptions();
        connoptions.Impersonation = System.Management.ImpersonationLevel.Impersonate;
        connoptions.Timeout = new TimeSpan(0, 0, 10); // 10 seconds
        System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\" + targetServer + @"\root\cimv2", connoptions);
        scope.Connect();

        string q = "select * from Win32_Product where name = '" + product + "' and IdentifyingNumber='"+guid+"' and version = '"+version+"'";

        System.Management.SelectQuery query = new System.Management.SelectQuery(q);

        System.Management.EnumerationOptions options = new System.Management.EnumerationOptions();
        options.EnumerateDeep = false;
        options.ReturnImmediately = false;
        options.DirectRead = true;

        using (System.Management.ManagementObjectSearcher searcher
            = new System.Management.ManagementObjectSearcher(scope, query, options))
        {
            using (System.Management.ManagementObjectCollection moc = searcher.Get())
            {
                if(moc == null || moc.Count == 0)
                {
                    throw new Exception("product Not Found");
                }

            }
        }

    }

Trying to find the product is installed in remote pC or not, it takes long time to execute,
is there any ways we can achieve this faster.

//usage:
//uninstall4("hostname", "productname", "{AC9C1263-2BA8-4863-BE18-01232375CE42}", "10.0.0.69");

    public void uninstall4(string targetServer, string product,string guid , string version)
    {
        //Connect to Server
        System.Management.ConnectionOptions connoptions = new System.Management.ConnectionOptions();
        connoptions.Impersonation = System.Management.ImpersonationLevel.Impersonate;
        connoptions.Timeout = new TimeSpan(0, 0, 10); // 10 seconds
        System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\" + targetServer + @"\root\cimv2", connoptions);
        scope.Connect();

        string q = "select * from Win32_Product where name = '" + product + "' and IdentifyingNumber='"+guid+"' and version = '"+version+"'";

        System.Management.SelectQuery query = new System.Management.SelectQuery(q);

        System.Management.EnumerationOptions options = new System.Management.EnumerationOptions();
        options.EnumerateDeep = false;
        options.ReturnImmediately = false;
        options.DirectRead = true;

        using (System.Management.ManagementObjectSearcher searcher
            = new System.Management.ManagementObjectSearcher(scope, query, options))
        {
            using (System.Management.ManagementObjectCollection moc = searcher.Get())
            {
                if(moc == null || moc.Count == 0)
                {
                    throw new Exception("product Not Found");
                }

            }
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2024-07-15 11:13:25

没有。 Win32_Product 类的提供程序必须在您查询时动态构建信息,而且它总是非常慢。

Nope. The provider for the Win32_Product class has to build the information dynamically when you query it, and it's always, always slow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文