I am trying to use C# to add a machine to an AD domain using WMI and I am having problems, the thing is if I comment out username and password parameters it works fine, but it adds the system to a workgroup instead of a domain, when I try to set the username and password it throws an "Attempted to access an unloaded AppDomain" exception. here's my code:
ManagementClass networkTask = new ManagementClass(connectionScope,
new ManagementPath("Win32_ComputerSystem"), new ObjectGetOptions());
ManagementObjectCollection moc = networkTask.GetInstances();
foreach (ManagementObject mo in moc)
{
ManagementBaseObject newDomain = mo.GetMethodParameters("JoinDomainOrWorkgroup");
newDomain["Name"] = domainName;
newDomain["Password"] = password;
newDomain["UserName"] = username;
newDomain["FJoinOptions"] = 3;
ManagementBaseObject rename = mo.InvokeMethod("JoinDomainOrWorkgroup", newDomain, null);
return 0;
}
I am connecting to the system using local administrator credentials
Related
I tried to connect to a remote PC and query for its processes but when I run the code, it got connected with my local PC and obtained its processes instead of the remote PC.
The code is
ManagementScope scope = new ManagementScope(#"\\remote-user\\root\\cimv2");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
You appear to be passing a username ("remote-user") instead of a hostname of the remote machine to your management scope. Change your code to e.g.:
ConnectionOptions options = new ConnectionOptions();
options.Password = "remoteUserPassword"; // you may want to avoid plain text password and use SecurePassword property instead
options.Username = "remote-user";
ManagementScope scope = new ManagementScope(#"\\remoteMachineHostname\root\cimv2", options);
(I assume that remote-user is Full computer name) change:
ManagementScope scope = new ManagementScope(#"\\remote-user\\root\\cimv2");
to:
ManagementScope scope = new ManagementScope(#"\\<FullComputerName>\root\cimv2");
another option:
ManagementScope scope = new ManagementScope("\\\\<FullComputerName>\\root\\cimv2");
See this link (it's Microsoft example)
Edit
if you want to connect with deffrent user you need to pass ConnectionOptions (see above link)
I am trying to connect (locally) to get a list of virtual machines and their properties. I have hacked some code I found, but my code is failing to connect so I can only assume that the connection string is wrong.
Using Server 2012, Hyper-V
private void listVirtualMachines() {
ManagementScope manScope = new ManagementScope(#"\\LOCALHOST\root\cimv2");
if (manScope.IsConnected) {
ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem");
// connect and set up our search
ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj);
ManagementObjectCollection vmCollection = vmSearcher.Get();
// loop through the machines
foreach (ManagementObject vm in vmCollection) {
// display VM details
LogString(vm["ElementName"].ToString());
LogString(vm["EnabledState"].ToString());
LogString(vm["Description"].ToString());
}
} else {
//WE END UP HERE EVERY TIME!
LogString("Cannot Connect to ManagementScope!");
}
} //funct
In Server 2012, Msvm_ComputerSystem is in the WMI namespace root\virtualization\v2 so the code should be:
ManagementScope manScope = new ManagementScope(#"\\.\root\virtualization\v2");
manScope.Connect();
I have some problem (c#).
I can retrieve some mainboard info from win32-baseboard but when I want to get Model
but an error accrued.
How can we get a list of installed software on windows (like xp).
How can we get a list of installed Peripheral device on Windows (with detail) (like scanner, webcam).
How to obtain total amount of ram (just) directly.
Use WMI (I suspect you already using it):
Model is blank. Try Manufacturer property. Also get the Product property to get the model.
Installed software: Get the Win32_Product class.
Try Win32_PnPSignedDriver class and iterate through.
Use Win32_ComputerSystem class and get TotalPhysicalMemory property.
Get WMIEXPLORER and play with it. LINK
Sample for C#:
If you require to connect to remote computer with credentials (strUsername and strPassword variables):
private ManagementScope CreateNewManagementScope(string server)
{
string serverString = #"\\" + server + #"\root\cimv2";
ManagementScope scope = new ManagementScope(serverString);
if (!chkUseCurrentUser.Checked)
{
ConnectionOptions options = new ConnectionOptions
{
Username = strUsername,
Password = strPassword,
Impersonation = ImpersonationLevel.Impersonate,
Authentication = AuthenticationLevel.PacketPrivacy
};
scope.Options = options;
}
return scope;
}
Get the services:
private void GetServicesForComputer(string computerName)
{
ManagementScope scope = CreateNewManagementScope(computerName);
SelectQuery query = new SelectQuery("select * from Win32_Service");
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
ManagementObjectCollection services = searcher.Get();
List<string> serviceNames =
(from ManagementObject service in services select service["Caption"].ToString()).ToList();
lstServices.DataSource = serviceNames;
}
}
catch (Exception exception)
{
lstServices.DataSource = null;
lstServices.Items.Clear();
lblErrors.Text = exception.Message;
Console.WriteLine(Resources.MainForm_GetServicesForServer_Error__ + exception.Message);
}
}
Some screens from wmiexplorer:
I'm trying to collect process information from remote machines using System.Management.
I'm impersonating an admin on the remote machine using something along these lines, but the following code throws an exception: "Access Denied".
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope(#"\\" + machine + #"\root\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("Select * from Win32_Process where ProcessId = "
+ procID.ToString());
ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);
string cmdLn = "";
foreach (ManagementObject mo in mos.Get())
{
cmdLn = (string)mo.GetPropertyValue("CommandLine");
}
However, if I supply the username and password to options, everything works fine.
I've verified that this code is executing as the impersonated context (which has sufficient permissions on the remote machine), so I'm not sure why it isn't working without the username/password passed.
Is it possible to authenticate successfully WITHOUT explicitly passing the user's credentials?
With WMI, when impersonating a specific user you must supply explicit credentials. The link you provided above states this with the variables lpszUsername and lpszPassword.
I am trying to shutdown and start the sql server on a remote computer (on the same network), i have used this code
ConnectionOptions options = new ConnectionOptions();
options.Username = userName;
options.Password = password;
ManagementScope scope =
new ManagementScope(
string.Format(#"\\{0}\root\cimv2", serverFullName),
options);
scope.Connect();
ObjectQuery query = new ObjectQuery(
string.Format(#"SELECT * FROM Win32_Process WHERE Name='{0}'",
processToTerminate));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
m.InvokeMethod("Terminate", null);
}
is there another way of doing that ?
how can i start the process (if Terminate close it)?
Thank you
What about using the ServiceController class? (see MSDN)
// just replace the params with your values
ServiceController sc = new ServiceController("SERVICENAME", "MACHINENAME");
if (sc.Status.Equals(ServiceControllerStatus.Running))
sc.Stop();
This should do the trick.
hth
It would seem a lot easier to just use the ServiceController class, that you can give a service name and computer name to, and then call methods such as Start and Stop.
Killing a process and stoping a service are two different things. A service could spawn other processes that will remain lingering. Also, you are effectively pulling the plug on the process. It isn't being given any time to stop gracefully, write everything to disk, etc.
Instead you should use the Win32_Service WMI object to find your service. This has a StartService and StopService method, which will allow you to stop and start it as you need.
Mind you, this WMI object is about services, not processes, so you will have to tweak your code to stop it by the service name, not the process name. Something like this:
ConnectionOptions options = new ConnectionOptions();
options.Username = userName;
options.Password = password;
ManagementScope scope = new ManagementScope(string.Format(#"\\{0}\root\cimv2", serverFullName), options);
scope.Connect();
ObjectQuery query = new ObjectQuery(string.Format(#"SELECT * FROM Win32_Service WHERE Name='{0}'",serviceToStop));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
m.InvokeMethod("StopService", null);
}
Then later on you can use InvokeMethod on StartService.
you can do some like this to start and stop your sql server
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "net start \"Sql Server (SQLEXPRESS)\"";
process.Start();