1
Answer

Need Help with Remote WMI Connection C#.Net

Dan Bibaud

Dan Bibaud

17y
5k
1
Hello,
I've been trying to get a remote connection to WMI working in a C# ASPX page. I know I'm doing something wrong, but I've seen too many variations of how to do it to make heads or tails out of anything. I'm new .NET so I'm sure I'm not doing it right. My code is below and it generates an "Invalid Parameter" Exception. If I remove the connection options completely the queries run, but on the webserver itself. Can someone show me how to make the remote connection by supplying credentials and school me by fixing any of my rookie mistakes? I'm compiling the .CS file into a DLL if that makes any difference. Thanks so much!


using System;

using System.Diagnostics;

using System.Management;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public class WMITest : System.Web.UI.Page

{
protected System.Web.UI.WebControls.Label mdLabel;


protected System.Web.UI.WebControls.Label cnLabel;
protected System.Web.UI.WebControls.Label memLabel;


protected System.Web.UI.WebControls.Label errorLabel;
public string Usr = "";

public void Page_Load(object sender, EventArgs e)
{

GetWmiData();

}

public void GetWmiData()
{

try

{

ConnectionOptions co = new ConnectionOptions();
co.Username = "user";

co.Password = "password";
co.Authority = "ntdlmdomain:domain";

ManagementScope ms = new ManagementScope("\\\\ip_address\\root\\cimv2", co);
ms.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");

ManagementObjectSearcher searcher =
new ManagementObjectSearcher(ms,query);

ManagementObjectCollection queryCollection = searcher.Get();foreach (ManagementObject queryObj in queryCollection)
{

cnLabel.Text = queryObj["Name"].ToString();

mdLabel.Text = queryObj["Model"].ToString();
memLabel.Text = queryObj["TotalPhysicalMemory"].ToString();

catch (ManagementException ex)
{

errorLabel.Text = ex.Message.ToString();

}

}



}
Answers (1)