Aldo
Aldo

Reputation: 1

Connect to SCCM server from REST API in C#

This is my first question here. I'm new to SCCM connections so I really don't know what is going on. I have my references to the following assemblies:

using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;

and I (roughly) use the following code:

WqlConnectionManager manager = new WqlConnectionManager(new SmsNamedValuesDictionary());
manager.Connect("myServerName");

If I run this code on a C# .NET Console application, everything works smoothly. If I run it on a Rest API developed on .NET Core, it throws an exception

Couldn't load assembly System.Windows.Forms

I have no idea why it needs such an assembly, but that is the situation.

Two things to keep in mind:

  1. the code belongs to the company where I'm working so there's no chance of me posting more code or screen captures.

  2. the current project makes it mandatory to use APIs that will be called from a web portal.

So, two questions:

  1. why does it work on console applications but not on Rest APIs?
  2. is it even recommended to connect to an SCCM server this way?

I copy-pasted some code from a WinForms application that is currently being used with zero problems. I was expecting to connect to the SCCM server as normal.

Upvotes: 0

Views: 672

Answers (1)

James Igoe
James Igoe

Reputation: 483

I am currently using C# to connect to the SCCM REST API for waking machines. As mentioned in my comment above, connecting with default credentials fails and what I code is different, but it needs to send a user ID and password.

    public bool Wake()
    {
        if (!string.IsNullOrEmpty(computerName))
        {
            ConnectionOptions options = new ConnectionOptions();
            options.Username = userId;
            options.Password = password;
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.EnablePrivileges = true;
            ManagementScope ms = new ManagementScope($"\\\\{siteServer}\\root\\SMS\\Site_SW1", options);

            SetResourceId(ms);
            ManagementBaseObject returnVal = ExecuteWithPassword(ms);

            if (returnVal == null)
            {
                return false;
            }
            else if (Convert.ToInt32(returnVal["numsleepers"]) >= 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else 
        { 
            return false; 
        }
    }

Subsequent code uses the scope to execute queries:

private void SetResourceId(ManagementScope ms)
{
    if (!string.IsNullOrEmpty(computerName))
    {
        var searcher = new ManagementObjectSearcher($"\\\\{siteServer}\\Root\\SMS\\Site_SW1", $"Select ResourceID from SMS_R_System Where NetBiosName = '{computerName}'")
        {
            Scope = ms
        };

        string ResourceID = searcher
            .Get()
            .Cast<ManagementObject>()
            .First()
            .Properties["ResourceID"].Value.ToString();

        if (!string.IsNullOrEmpty(ResourceID))
        {
            resourceId = ResourceID;
        }
    }
}

Upvotes: 0

Related Questions