I wrote the following code to change the user account and password associated with a Windows Service. How can I modify this code to be able to perform the same operation on a remote system?
static void Main(string[] args)
{
string serviceName = "DummyService";
string username = ".\\Service_Test2";
string password = "Password1";
ServiceController sc = new ServiceController(serviceName);
Console.WriteLine(sc.Status.ToString());
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
Thread.Sleep(2000);
sc.Refresh();
Console.WriteLine(sc.Status.ToString());
string objPath = string.Format("Win32_Service.Name='{0}'", serviceName);
using (ManagementObject service = new ManagementObject(new ManagementPath(objPath)))
{
object[] wmiParams = new object[11];
wmiParams[6] = username;
wmiParams[7] = password;
service.InvokeMethod("Change", wmiParams);
}
Thread.Sleep(2000);
Console.WriteLine(sc.Status.ToString());
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
Thread.Sleep(2000);
sc.Refresh();
Console.WriteLine(sc.Status.ToString());
}
Use ServiceController constructor overload that allows target machine-name to be specified
Modify WMI object path to include the target server.
new ManagementPath(
"\\\\ComputerName\\root" +
"\\cimv2:Win32_Service.Name='{0}'");
Make sure your user/password have sufficient rights on target machine, change those if not.
Related
We referred stackOverflow and a few other sites to create a website on IIS using console application. It worked after we run the console with administration permission. Code does create an Application pool and host site on a given port. Same code when we tried on asp.net web application it completed its execution but we did not found metadata on IIS.
Here is a code that works on console..
try
{
ServerManager server = new ServerManager();
ApplicationPool myApplicationPool = null;
//we will first check to make sure that this pool does not already exist
//since the ApplicationPools property is a collection, we can use the Linq FirstOrDefault method
//to check for its existence by name
if (server.ApplicationPools != null && server.ApplicationPools.Count > 0)
{
if (server.ApplicationPools.FirstOrDefault(p => p.Name == "TestPool") == null)
{
//if the pool is not already there we will create it
myApplicationPool = server.ApplicationPools.Add("TestPool");
}
else
{
//if we find the pool already there, we will get a referecne to it for update
myApplicationPool = server.ApplicationPools.FirstOrDefault(p => p.Name == "TestPool");
}
}
else
{
//if the pool is not already there we will create it
myApplicationPool = server.ApplicationPools.Add("TestPool");
}
if (myApplicationPool != null)
{
//for this sample, we will set the pool to run under the NetworkService identity
myApplicationPool.ProcessModel.IdentityType =
ProcessModelIdentityType.NetworkService;
//for this sample, we will set the pool to run under the identity of a specific user
//myApplicationPool.ProcessModel.IdentityType =
ProcessModelIdentityType.SpecificUser;
//myApplicationPool.ProcessModel.UserName = UserName;
//myApplicationPool.ProcessModel.Password = Password;
//we set the runtime version
myApplicationPool.ManagedRuntimeVersion = "v4.0";
//we save our new ApplicationPool!
server.CommitChanges();
}
//Create website
if (server.Sites != null && server.Sites.Count > 0)
{
//we will first check to make sure that the site isn't already there
if (server.Sites.FirstOrDefault(s => s.Name == "MySite") == null)
{
//we will just pick an arbitrary location for the site
string path = #"C:\inetpub\Custom";
//we must specify the Binding information
string ip = "*";
string port = "98";
string hostName = "*";
string bindingInfo = string.Format(#"{0}:{1}:{2}", ip, port, hostName);
//add the new Site to the Sites collection
Site site = server.Sites.Add("MySite", "http", bindingInfo, path);
//set the ApplicationPool for the new Site
site.ApplicationDefaults.ApplicationPoolName = myApplicationPool.Name;
//save the new Site!
server.CommitChanges();
Console.WriteLine("Web site created successfully...");
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine()
}
Here site gets listed in IIS as well.
Now when tried the same code on the web application, it does not create anything on IIS. when we inspect the server manager object we found that the Application pool list was coming from the project's
applicationhost.config
file which is located on .vs hidden folder.
We installed the latest IIS on the local machine for a test, is there any changes needed to get it to work on the web as well. (: we are new to IIS stuff)
You could try below code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;
namespace IISTest
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Do you want to create an Application Pool:y/n");
string response = Console.ReadLine();
if (response.ToString() == "y")
{
Console.Write("Please enter Application Pool Name:");
string poolname = Console.ReadLine();
bool isEnable32bit = false;
ManagedPipelineMode mode = ManagedPipelineMode.Classic;
Console.Write("Need to enable 32 bit on Windows 64 bit?y/n [Applicable for 64 bit OS]: y/n?");
string enable32bit = Console.ReadLine();
if (enable32bit.ToLower() == "y")
{
isEnable32bit = true;
}
Console.Write("Please select Pipeline Mode: 1 for Classic, 2 for Integrated:");
string pipelinemode = Console.ReadLine();
if (pipelinemode.ToLower() == "2")
{
mode = ManagedPipelineMode.Integrated;
}
Console.Write("Please select Runtime Version for Application Pool: 1 for v2.0, 2 for v4.0:");
string runtimeVersion = Console.ReadLine() == "1" ? "v2.0" : "v4.0";
CreateAppPool(poolname, isEnable32bit, mode, runtimeVersion);
Console.WriteLine("Application Pool created successfully...");
}
Console.WriteLine("Do you want to create a website:y/n");
response = Console.ReadLine();
if (response.ToString() == "y")
{
Console.Write("Please enter website name:");
string websiteName = Console.ReadLine();
Console.Write("Please enter host name:");
string hostname = Console.ReadLine();
Console.Write("Please enter physical path to point for website:");
string phypath = Console.ReadLine();
Console.WriteLine("Application pool Name:");
foreach (var pool in new ServerManager().ApplicationPools)
{
Console.WriteLine(pool.Name);
}
Console.WriteLine("");
Console.Write("Please enter Application pool Name for web site:");
string poolName = Console.ReadLine();
CreateIISWebsite(websiteName, hostname, phypath, poolName);
Console.WriteLine("Web site created successfully...");
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
private static void CreateIISWebsite(string websiteName, string hostname, string phyPath, string appPool)
{
ServerManager iisManager = new ServerManager();
iisManager.Sites.Add(websiteName, "http", "*:80:" + hostname, phyPath);
iisManager.Sites[websiteName].ApplicationDefaults.ApplicationPoolName = appPool;
foreach (var item in iisManager.Sites[websiteName].Applications)
{
item.ApplicationPoolName = appPool;
}
iisManager.CommitChanges();
}
private static void CreateAppPool(string poolname,bool enable32bitOn64, ManagedPipelineMode mode,string runtimeVersion="v4.0")
{
using (ServerManager serverManager = new ServerManager())
{
ApplicationPool newPool = serverManager.ApplicationPools.Add(poolname);
newPool.ManagedRuntimeVersion = runtimeVersion;
newPool.Enable32BitAppOnWin64 = true;
newPool.ManagedPipelineMode = mode;
serverManager.CommitChanges();
}
}
}
}
note: do not forget to add a reference to Microsoft.Web.Administration.
I've got a problem with setting the Admin privileges in a propper way; I have already tried ServiceController & ServiceControllerPermission, and it failed, cannot find any solution on the internet which will solve my problem. Code written below:
public class Restarter
{
public string Run(string serviceName)
{
var ctlPrms = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, serviceName);
ctlPrms.Assert();
var srvCtl = new ServiceController(serviceName, Environment.MachineName);
srvCtl.Refresh();
var msg = $"At the beginning, Service: {srvCtl.DisplayName} has status of: {srvCtl.Status}!\n";
if (srvCtl.Status.Equals(ServiceControllerStatus.Running))
{
msg = msg + "Attempting to stop running service\n";
srvCtl.Stop();
srvCtl.WaitForStatus(ServiceControllerStatus.Stopped);
}
else
{
msg = msg + "Attempting to start the service\n";
srvCtl.Start();
srvCtl.WaitForStatus(ServiceControllerStatus.Running);
}
msg = msg + $"At the end, Service: {srvCtl.DisplayName} has status of: {srvCtl.Status}!";
return msg;
}
}
I will be more than gratefull for help with that, I'm already pulling my hair out because of it
I have a code that tries to access the services of another computer.
try
{
var serviceName = "MyService";
var ip = "10.10.11.16";
var username = "SomeUser";
var password = "APassword";
var connectoptions = new ConnectionOptions();
connectoptions.Impersonation = ImpersonationLevel.Impersonate;
connectoptions.Authentication = AuthenticationLevel.Packet;
connectoptions.EnablePrivileges = true;
connectoptions.Username = username;
connectoptions.Password = password;
var scope = new ManagementScope("\\\\10.10.11.16\\root\\cimv2");
scope.Options = connectoptions;
var query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
var collection = searcher.Get();
foreach (ManagementObject service in collection.OfType<ManagementObject>())
{
if (service["started"].Equals(true))
{
service.InvokeMethod("StopService", null);
BtnStartStop.Content = "Stop";
LblService.Content = serviceName;
LblServiceStatus.Content = "Stopped";
}
else
{
service.InvokeMethod("StartService", null);
BtnStartStop.Content = "Stop";
LblService.Content = serviceName;
LblServiceStatus.Content = "Running";
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Will this work on Server and client only? Won't this work on regular pc to another regular pc? Each time I run this when I get to the part of:
var collection = searcher.Get();
I get an error of
Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))
Do you have an idea on to make this work? Thank you.
STEPS DONE SO FAR
Followed the instruction on
https://learn.microsoft.com/en-us/windows/win32/wmisdk/connecting-to-wmi-remotely-starting-with-vista
typed in the cmd with admin privilege
netsh advfirewall firewall set rule group="windows management instrumentation (wmi)" new enable=yes
I even turned off the firewall just to be sure.
edited the registry of the pc I am connecting to this:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WBEM\CIMOM\AllowAnonymousCallback
Data type
REG\_DWORD
As for the antivirus, the pc I am connecting to does not have any anti virus.
I still get the same error.
As #colosso pointed out, you are receiving that error message because you do not have permission on the remote host to connect to the WMI service.
You should follow the instructions here to ensure the remote host is configured to allow your connection.
I'm trying to write a small command line application with C# that will prompt for a username and a password that will be used to login to several remote computers that are sitting on the same network/domain and start a local session.
I've tried connecting to a remote computer and to query the remote PC's operating system info with the following code:
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope(#"\\REMOTE_COMPUTER_NAME");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Computer Name : {0}", m["csname"]);
Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}", m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
}
However, this only returns information on the local PC that I'm working on and not on the remote PC.
Am I overlooking something with this code? And is there an appropriate approach to accomplish what I am trying to do?
I don't have remote machine right now to give you the working example, but you can try this. advapi32.logonuser
Example:
[DllImport("advapi32.dll")]
public static extern bool LogonUser(string name, string domain, string pass,
int logType, int logpv, out IntPtr pht);
IntPtr ptr;
// LogonUser("username", "remotemachine", "password", 2, 0, out ptr);
LogonUser("username", "remotemachine", "password", 9, 0, out ptr);
WindowsIdentity windowsIdentity = new WindowsIdentity(ptr);
var impersonationContext = windowsIdentity.Impersonate();
// your code goes here...
impersonationContext.Undo();
This logon type allows the caller to clone its current token and
specify new credentials for outbound connections. The new logon
session has the same local identifier but uses different credentials
for other network connections. NOTE: This logon type is supported
only by the LOGON32_PROVIDER_WINNT50 logon provider. NOTE: Windows NT:
This value is not supported.
http://www.pinvoke.net/default.aspx/advapi32.logonuser
Edit
Give it a try cassia
ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetRemoteServer("servername"))
{
server.Open();
foreach (ITerminalServicesSession session in server.GetSessions())
{
Console.WriteLine("Hi there, " + session.UserAccount + " on session " + session.SessionId);
Console.WriteLine("It looks like you logged on at " + session.LoginTime +
" and are now " + session.ConnectionState);
}
}
You have to use ConnectionOptions and pass it to the ManagementScope
public void GetSystemInformation(string _yourDomain, string _hostName, string _userName, SecureString _password)
{
ManagementScope Scope = null;
string computerName = _hostName;
string userName = _userName;
SecureString password = _password;
ManagementObjectCollection collection = null;
try
{
SelectQuery query = new SelectQuery("SELECT * FROM Win32_OperatingSystem");
//string query = "SELECT * FROM Win32_NetworkAdapterConfiguration" + " WHERE IPEnabled = 'TRUE'";
var options = new ConnectionOptions
{
EnablePrivileges = false,
Impersonation = ImpersonationLevel.Impersonate,
Username = _userName,
SecurePassword = _password,
Authority = "ntlmdomain:" + _yourDomain
};
Scope.Options = options;
Scope.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Scope, query);
collection = searcher.Get();
//Do something with the collection
}
catch (ManagementException ex)
{
Console.WriteLine(ex.Message);
}
catch (UnauthorizedAccessException ex)
{
throw new ArgumentException(ex.Message);
}
}
private static SecureString CreateSecuredString(string pw)
{
var secureString = new SecureString();
foreach (var c in pw)
{
secureString.AppendChar(c);
}
return secureString;
}
You might have to try some different stats with the variables EnablePrivileges and Impersonation
edit:
If you want to get your information from your pc (local) than you dont have to pass the options to the scope.
Does anyone know if there is a way to install a Windows service created in C# without making an installer?
I include a class that does the installation for me. I call the application using command line parameters to install or uninstall the app. I have also in the past included a prompt to the user whether they wanted the service installed when started directly from the command line.
Here's the class I use:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;
namespace [your namespace here]
{
class IntegratedServiceInstaller
{
public void Install(String ServiceName, String DisplayName, String Description,
System.ServiceProcess.ServiceAccount Account,
System.ServiceProcess.ServiceStartMode StartMode)
{
System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
ProcessInstaller.Account = Account;
System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
string processPath = Process.GetCurrentProcess().MainModule.FileName;
if (processPath != null && processPath.Length > 0)
{
System.IO.FileInfo fi = new System.IO.FileInfo(processPath);
//Context = new System.Configuration.Install.InstallContext();
//Context.Parameters.Add("assemblyPath", fi.FullName);
//Context.Parameters.Add("startParameters", "Test");
String path = String.Format("/assemblypath={0}", fi.FullName);
String[] cmdline = { path };
Context = new System.Configuration.Install.InstallContext("", cmdline);
}
SINST.Context = Context;
SINST.DisplayName = DisplayName;
SINST.Description = Description;
SINST.ServiceName = ServiceName;
SINST.StartType = StartMode;
SINST.Parent = ProcessInstaller;
// http://bytes.com/forum/thread527221.html
// SINST.ServicesDependedOn = new String[] {};
System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
SINST.Install(state);
// http://www.dotnet247.com/247reference/msgs/43/219565.aspx
using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(#"SYSTEM\CurrentControlSet\Services\{0}", SINST.ServiceName), true))
{
try
{
Object sValue = oKey.GetValue("ImagePath");
oKey.SetValue("ImagePath", sValue);
}
catch (Exception Ex)
{
// System.Console.WriteLine(Ex.Message);
}
}
}
public void Uninstall(String ServiceName)
{
System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext("c:\\install.log", null);
SINST.Context = Context;
SINST.ServiceName = ServiceName;
SINST.Uninstall(null);
}
}
}
And here's how I call it:
const string serviceName = "service_name";
const string serviceTitle = "Service Title For Services Control Panel Applet";
const string serviceDescription = "A longer description of what the service does. This is used by the services control panel applet";
// Install
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
Inst.Install(serviceName, serviceTitle, serviceDescription,
// System.ServiceProcess.ServiceAccount.LocalService, // this is more secure, but only available in XP and above and WS-2003 and above
System.ServiceProcess.ServiceAccount.LocalSystem, // this is required for WS-2000
System.ServiceProcess.ServiceStartMode.Automatic);
// Uninstall
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
Inst.Uninstall(serviceName);
You could try the windows sc command
C:\WINDOWS\system32>sc create
DESCRIPTION:
SC is a command line program used for communicating with the NT Service Controller and services.
You can use installutil.
From the command line:
installutil YourWinService.exe
This utility is installed with the .NET Framework