如果有人需要知道的话,下面我将从一个非管理员用户帐户中学到的关于启动/停止 Windows 服务的所有知识整合在一起。
主要有两种启动/停止 Windows 服务的方式。
1. 通过登录 Windows 用户帐户直接访问服务。
2. 使用网络服务帐户通过 IIS 访问服务。
用于启动/停止服务的命令行命令:
C:/> net start <SERVICE_NAME>
C:/> net stop <SERVICE_NAME>
C # 启动/停止服务代码:
ServiceController service = new ServiceController(SERVICE_NAME);
//Start the service
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10.0));
}
//Stop the service
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10.0));
}
注一:
当通过 IIS 访问服务时,创建一个 Visual Studio C # ASP.NET Web 应用程序并将代码放入其中。将 WebService 部署到 IIS 根文件夹(C: inetpub wwwroot)就可以了。
访问它的网址 http:///。
1. 直接存取法
如果发出命令或运行代码的 Windows 用户帐户是非管理帐户,则需要将特定用户帐户的特权设置为该用户帐户,以便它能够启动和停止 Windows 服务。你要这么做。
登录到计算机上的管理员帐户,该帐户具有要从中启动/停止服务的非管理员帐户。打开命令提示符并执行以下命令:
A description of one part of above command is as follows:
D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)
It has the default owner, default group, and it has the Security descriptor control flags (A;;CCLCSWRPWPDTLOCRRC;;;SY):
ace_type - "A": ACCESS_ALLOWED_ACE_TYPE,
ace_flags - n/a,
rights - CCLCSWRPWPDTLOCRRC, please refer to the Access Rights and Access Masks and Directory Services Access Rights
CC: ADS_RIGHT_DS_CREATE_CHILD - Create a child DS object.
LC: ADS_RIGHT_ACTRL_DS_LIST - Enumerate a DS object.
SW: ADS_RIGHT_DS_SELF - Access allowed only after validated rights checks supported by the object are performed. This flag can be used alone to perform all validated rights checks of the object or it can be combined with an identifier of a specific validated right to perform only that check.
RP: ADS_RIGHT_DS_READ_PROP - Read the properties of a DS object.
WP: ADS_RIGHT_DS_WRITE_PROP - Write properties for a DS object.
DT: ADS_RIGHT_DS_DELETE_TREE - Delete a tree of DS objects.
LO: ADS_RIGHT_DS_LIST_OBJECT - List a tree of DS objects.
CR: ADS_RIGHT_DS_CONTROL_ACCESS - Access allowed only after extended rights checks supported by the object are performed. This flag can be used alone to perform all extended rights checks on the object or it can be combined with an identifier of a specific extended right to perform only that check.
RC: READ_CONTROL - The right to read the information in the object's security descriptor, not including the information in the system access control list (SACL). (This is a Standard Access Right, please read more http://msdn.microsoft.com/en-us/library/aa379607(VS.85).aspx)
object_guid - n/a,
inherit_object_guid - n/a,
account_sid - "SY": Local system. The corresponding RID is SECURITY_LOCAL_SYSTEM_RID.
现在我们需要做的是为我们想要的组或用户设置适当的权限来启动/停止 Windows 服务。在这种情况下,我们需要当前的非管理用户能够启动/停止服务,所以我们将设置该用户的权限。为此,我们需要该特定 Windows 用户帐户的 SID。要获取它,请打开 Registry (Start > regedit)并找到以下注册表项。
//LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList RegistryKey
RegistryKey profileList = Registry.LocalMachine.OpenSubKey(keyName);
//Get a list of SID corresponding to each account on the computer
string[] sidList = profileList.GetSubKeyNames();
foreach (string sid in sidList)
{
//Based on above names, get 'Registry Keys' corresponding to each SID
RegistryKey profile = Registry.LocalMachine.OpenSubKey(Path.Combine(keyName, sid));
//SID
string strSID = sid;
//UserName which is represented by above SID
string strUserName = (string)profile.GetValue("ProfileImagePath");
}
F : Full Control
R : Generic Read
W : Generic Write
X : Generic eXecute
L : Read controL
Q : Query Service Configuration
S : Query Service Status
E : Enumerate Dependent Services
C : Service Change Configuration
T : Start Service
O : Stop Service
P : Pause/Continue Service
I : Interrogate Service
U : Service User-Defined Control Commands
因此,通过指定 PTO,我允许 工作用户暂停/继续、启动和停止 w3svc 服务。
编辑 : < em > 更新到 web.archive. org 的链接,因为原始的 MS 链接已经死亡。