This code works fine when opening two other VMs but gives me an RPC error on
opening a computer on a different domain. At the moment I am using the ConnectionOptions class to connect to the remote computer
`options.Username = dif_users[i]; // Assume correct user name
options.Password = dif_passwords[i]; // Assume correct password
if (i == 2) // To execute for the third VM. (i = 0 in the beginning)
{
//
options.Authentication = AuthenticationLevel.Packet;
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;
options.Authority = "ntlmdomain:DIFFERENT_DOMAIN";
}
ManagementScope scope = new ManagementScope("\\\\" + comps + "\\root\\cimv2", options);
scope.Connect(); // RPC server is unavaialaible
`
Related
When I try to execute the below code, I get the error - RPC server is unavailable.
string username = "****";
string password = "****";
server = comboBox1.SelectedItem.ToString();
service = "****";
ServiceController windowsservice = new ServiceController(service, server);
try
{
ConnectionOptions connectopt = new ConnectionOptions();
connectopt.Username = username;
connectopt.Password = password;
connectopt.EnablePrivileges = true;
connectopt.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope(#"\\'"+server+"'\\root\\cimv2", connectopt);
scope.Connect();
}
But when I hardcode the value of server name in the line
ManagementScope scope = new ManagementScope(#"\\'"+server+"'\\root\\cimv2", connectopt);
it works fine with no errors
Any help would be much appreciated
I'm trying to get status of a remote windows service. I use code like this, but nothing happens. Labels have not get any value. Help my please!
ConnectionOptions options = new ConnectionOptions();
options.Password = "password";
options.Username = "username";
options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope("\\\\computer_name\\root\\cimv2", options);
scope.Connect();
ServiceController sc = new ServiceController("service_name", "computer_name");
label1.Text = sc.ServiceName;
label2.Text = sc.Status.ToString();
using System;
using System.Management;
public class Class1
{
public static void Main()
{
string strComputer = string.Format(#"machineName.domainname\root\cimv2");
ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Packet;
options.Authority = "ntlmdomain:InsTIL.com:InsTIL.com";
options.Username = "usr";
options.Password = "pwd";
ManagementScope oMs = new ManagementScope(strComputer, options);
SelectQuery query =new SelectQuery("Select * From Win32_Directory Where Name ='"+string.Format(#"C:\Scripts")+"'");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,query);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
if (oReturnCollection.Count < 1)
{
Console.WriteLine("Folder does not exist");
}
else
{
Console.WriteLine("Folder does exist");
}
}
}
I'm trying to connect to remote machine and checking existence of folder.But I'm getting below mentioned error.
I tried and incorporated changes discussed in remote wmi connection c# - invalid parameter error
Program abruptly stops working and throws below error:
Unhandled Exception: System.Management.ManagementException: Invalid parameter
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
at System.Management.ManagementPath.CreateWbemPath(String path)
at System.Management.ManagementPath..ctor(String path)
at Class1.Main()
You need backslashes before your machine name. Change this:
string strComputer = string.Format(#"machineName.domainname\root\cimv2");
to this:
string strComputer = string.Format(#"\\machineName.domainname\root\cimv2");
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Username = _username;
connOptions.Password = _password;
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.Authentication = AuthenticationLevel.PacketPrivacy;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(_server, connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["Name"] = "TESTER";
inParams["Owner"] = "Tester";
inParams["Command"] = command;
inParams["StartTime"] = "********171000.000000-300";
I'm tyring to connect to a remote system to create a scheduled task. I can create the scheduled tasks, but its being created with user - SYSTEM. I want it to be created under my user. I tried using the properties like 'Owner' and 'Name' eg:
inParams["Owner"] = ;
inParams["Name"] = ;
But they throw a ManagementException, "Not Found". Does anyone know how I can do this, or what might be wrong that I'm doing here...
Thanks
Creating a scheduled job with the Win32_ScheduledJob WMI class is equivalent to create job using the AT command. The AT service normally run under the Local\System account or the NetworkService Account. so when you uses this class your jobs are created using one of these accounts for more info about this topic you can check the remarks part of the MSDN documentation.
I have searched through numerous questions about how to convert VBS to C# and all that good stuff.
The issue that I'm having is that with the VBS code (see below) when the process is executed on the remote machine it's run with the SYSTEM account.
When I execute with C# it's run with my credentials (or whomever runs the C# program).
The VBS seems to be more reliable in getting remote installs to go through which is what i need it for.
I wanted to switch to C# so I could make a more user friendly GUI for the program.
Anyone know how to get C# to run WMI with the SYSTEM account?
VBS Code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
'add the scheduled job to be run
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
C# code:
static public int RemoteAdmin(string remoteMachine, string runFile)
{
try
{
ManagementPath run = new ManagementPath(#"\\" + remoteMachine + #"\root\cimv2:Win32_process");
ManagementClass man = new ManagementClass(run);
man.InvokeMethod("Create", new Object[] { runFile });
return 0;
}
catch
{
MessageBox.Show("Error in remote execution");
return 1;
}
}
You need to setup the ConnectionOptions
ConnectionOptions wmiConnOpts = new ConnectionOptions();
wmiConnOpts.Impersonation = ImpersonationLevel.Impersonate;
wmiConnOpts.Authentication = System.Management.AuthenticationLevel.Default;
wmiConnOpts.EnablePrivileges = true;
ManagementScope wmiLoc =
new ManagementScope(String.Format(#"\\{0}\root\cimv2", remoteMachine ),
wmiConnOpts);
ManagementPath wmiProcPath = new ManagementPath("Win32_ScheduledJob");
ManagementClass wmiProc = new ManagementClass(wmiLoc, wmiProcPath, null);
wmiProc.InvokeMethod("Create", new Object[] { runFile });
This is probably idiot error on my part but the error I was getting was because I didn't specify a time (UTC format) for it to start the scheduled job
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.Authentication = AuthenticationLevel.PacketPrivacy;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(#"\\" + remoteMachine + #"\ROOT\CIMV2"), connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["Command"] = runFile;
string StartTime = DateTimetoUTC(DateTime.Now.AddMinutes(1));
inParams["StartTime"] = StartTime;
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);