API to access WMI on SCCM

To create an ASP.NET API to access WMI on SCCM, you will first need to make sure that you have the necessary prerequisites installed on your system. This includes the .NET Framework and the ASP.NET Core runtime. You will also need to install the System.Management.Automation NuGet package, which provides the API for accessing WMI in ASP.NET Core.

Once you have all of the necessary prerequisites installed, you can create a new ASP.NET Core web application in Visual Studio by going to File > New > Project. Select the “ASP.NET Core Web Application” template, and choose a name and location for your project.

In the next screen, select the “API” template and make sure that the “Enable Docker Support” option is unchecked. This will create a basic ASP.NET Core API project with the necessary dependencies and configuration settings.

Next, you will need to add the System.Management.Automation NuGet package to your project. In Visual Studio, right-click on the project in the Solution Explorer and select “Manage NuGet Packages”. In the NuGet Package Manager, search for “System.Management.Automation” and install the package.

Now you can add the code for accessing WMI on SCCM. First, you will need to add a reference to the System.Management.Automation namespace in your project. You can do this by adding the following using statement at the top of your code file:

using System.Management.Automation;

Next, you can use the PowerShell class from the System.Management.Automation namespace to access WMI on SCCM. Here is an example of how you might do this:

// Create a new PowerShell instance
using (PowerShell ps = PowerShell.Create())
    // Add a command to get the WMI class
    ps.AddParameter("Class", "SMS_Site");

    // Execute the command and get the results
    var results = ps.Invoke();

    // Loop through the results and print the properties of each object
    foreach (var result in results)
        Console.WriteLine("Site Code: {0}", result.SiteCode);
        Console.WriteLine("Site Name: {0}", result.SiteName);
        Console.WriteLine("Site Server: {0}", result.SiteServer);

This code creates a new instance of the PowerShell class and adds a command to get the SMS_Site WMI class. It then executes the command and loops through the results, printing the SiteCode, SiteName, and SiteServer properties of each object.

You can use this basic approach to access any WMI class on SCCM using ASP.NET Core. Of course, you will need to adjust the code to match the specific WMI class and properties that you want to access.