restart a service in c# - c#

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

Related

How can I read the CPU Load of a remote PC with CIM session instead of WMI? I have to use the admin credentials of the remote PC

I am using the following code for reading CPU Load on remote PC with Win XP. I have to use the credentials on the remote PC. It is working fine. But how can I use CIM session instead of WMI?
public void Read_CPU_Load_WMI()
{
WMI_path = "\\\\" + TagService.IP_to_Connect + "\\root\\cimv2";
System.Management.ConnectionOptions options = new System.Management.ConnectionOptions();
options.Username = username;
options.Password = password;
ManagementScope scope = new ManagementScope(WMI_path, options);
scope.Connect();
SelectQuery query = new SelectQuery("select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor where Name='_Total'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
foreach (ManagementObject queryObj in searcher.Get())
{
CPU_Load_WMI_int = Convert.ToInt16(queryObj["PercentProcessorTime"]);
CPU_Load_WMI_str = queryObj["PercentProcessorTime"].ToString();
}
}
I have started as following but cannot finish it
public void Read_CPU_Load_CIM
{
WMI_path = "\\\\" + TagService.IP_to_Connect + "\\root\\cimv2";
System.Management.ConnectionOptions options = new System.Management.ConnectionOptions();
options.Username = username;
options.Password = password;
string Namespace = #"root\cimv2";
string OSQuery = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor where Name='_Total'";
CimSession mySession = CimSession.Create(WMI_path);
IEnumerable<CimInstance> queryInstance = mySession.QueryInstances(Namespace, "WQL", OSQuery);
//CPU_Load_WMI_int = ???
}

Error Connecting to a VM on a different domain

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
`

Connect to OrientDB via C#.net

I am using Orient-db 2.2.13 and a VisualStudio2015 and I am trying to perform a simple "test connection" method from c#.net to an existing orientDB I have.
In java it is very simple to perform:
OrientGraphFactory factory = new OrientGraphFactory(remoteUrl, user, password, false);
result = factory.getNoTx().command(new OCommandSQL("select....")).execute();
But in C#.Net it seems to be a less easy.
All I got so far is this (and it is NOT working)
OServer _server = new OServer(_hostname, _port, _rootUserName, _rootUserPassword);
ODatabase odb = new ODatabase(_hostname, _port, _DBname, ODatabaseType.Graph, _rootUserName, _rootUserPassword);
can you help me please?
Just tried today using OrientDB 2.2.16
ConnectionOptions opts = new ConnectionOptions();
opts.HostName = "localhost";
opts.UserName = "root";
opts.Password = "mypassword";
opts.Port = 2424;
opts.DatabaseName = "mydatabasename";
opts.DatabaseType = ODatabaseType.Graph;
database = new ODatabase(opts);
Console.Write(database.Size);
Try staarting using this template and then execute command using
database.execute(..);
this works for me (VS2015, Orient 2.2.5, OrientDB-Net.binary.Innov8tive 0.1.12)
using (ODatabase db = new ODatabase(server, port, dbname, ODatabaseType.Graph, user, pw))
{
string rid="#12:0";
OVertex v1 = db.Query<OVertex>($"select * from {rid}")[0];
var myfield = v1.GetField<string>("string_field_name");
}

How to get status of remote windows service

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();

WMI connection error C#

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");

Categories