我目前正在编写一个与SCCM交互并查询设备集合的小程序。但是因为我使用的是Asp.Net 5,所以SCCM的AdminUI.WqlQueryEngine和Microsoft.ConfigurationManagement.ManagementProvider似乎是不兼容的,因为当我尝试连接时:
//' Connect to SMS Provider
SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
WqlConnectionManager connection = new WqlConnectionManager(namedValues);
connection.Connect(serverName);我收到以下错误:
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:
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的最好方式?
谢谢
发布于 2021-05-05 23:16:48
我知道现在有点晚了,但是如果您必须让它与WqlConnectionManager一起工作,您可以编辑您的项目csproj,并将TargetFramework更改为net5.0-windows并包含标记<UseWindowsForms>true</UseWindowsForms>
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>这将在您的应用程序中包含windows窗体,因此您需要决定是否值得这样做。我认为,正确的方法应该是像家长问题一样使用WMI。但重写可能不是一种选择。
https://stackoverflow.com/questions/66283663
复制相似问题