首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从SCCM中的一台设备获取所有集合

从SCCM中的一台设备获取所有集合
EN

Stack Overflow用户
提问于 2021-02-20 03:10:56
回答 1查看 86关注 0票数 0

我目前正在编写一个与SCCM交互并查询设备集合的小程序。但是因为我使用的是Asp.Net 5,所以SCCM的AdminUI.WqlQueryEngine和Microsoft.ConfigurationManagement.ManagementProvider似乎是不兼容的,因为当我尝试连接时:

代码语言:javascript
复制
//' Connect to SMS Provider
                SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
                WqlConnectionManager connection = new WqlConnectionManager(namedValues);
                connection.Connect(serverName);

我收到以下错误:

代码语言:javascript
复制
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.'

到目前为止,我的工作就是使用WMI:

代码语言:javascript
复制
public static List<CollectionSccmCM> GetCMDeviceCollections(string hostname, string siteCode, string siteServer= null){

        List<CollectionSccmCM> collectionList = new List<CollectionSccmCM>();

        try{
            string Namespace;

            if(string.IsNullOrEmpty(siteServer)){
                Namespace = @"Root\SMS\Site_" + siteCode;
            } else {
                Namespace = (@"\\" + siteServer + @"\root\SMS\Site_" + siteCode);
            }
            
            ManagementScope scope = new ManagementScope(Namespace);

            scope.Connect();

            ObjectQuery query = new ObjectQuery(String.Format("SELECT SMS_Collection.Name, SMS_Collection.CollectionID, SMS_Collection.CollectionType FROM SMS_FullCollectionMembership, SMS_Collection where name = '{0}' and SMS_FullCollectionMembership.CollectionID = SMS_Collection.CollectionID", hostname));

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            ManagementObjectCollection queryCollection = searcher.Get();

            if (queryCollection != null)
            {
                foreach (ManagementObject obj in queryCollection)
                {
                    CollectionSccmCM coll = new CollectionSccmCM()
                        {
                            Name = obj["Name"].ToString(),
                            CollectionID = obj["CollectionID"].ToString()
                        };
                        collectionList.Add(coll);
                }
            }
        }
        catch (ManagementException ex)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){
                EventService.WriteEventLog(String.Format("Unathorized access exception thrown: {0}", ex.Message), EventLogEntryType.Error);
            }
        }
        catch (Exception ex)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){
                EventService.WriteEventLog(String.Format("Unhandled expection thrown: {0}", ex.Message), EventLogEntryType.Error);
            }
        }

        return collectionList;

    }

不管怎样,有人在使用这些dll时遇到了类似的问题吗?或者这是直接使用ManagementObjectSearcher的最好方式?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-05-05 23:16:48

我知道现在有点晚了,但是如果您必须让它与WqlConnectionManager一起工作,您可以编辑您的项目csproj,并将TargetFramework更改为net5.0-windows并包含标记<UseWindowsForms>true</UseWindowsForms>

代码语言:javascript
复制
<PropertyGroup>
  <TargetFramework>net5.0-windows</TargetFramework>
  <UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

这将在您的应用程序中包含windows窗体,因此您需要决定是否值得这样做。我认为,正确的方法应该是像家长问题一样使用WMI。但重写可能不是一种选择。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66283663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档