我知道这个问题已经有多种不同的问题,但我目前在我的智慧中。和谷歌搜索能力'结束。我编写了一个Console应用程序来重新启动远程计算机上的服务。
我们域中的其他用户也需要能够将此应用程序用于此目的,即使他们的凭据不足以重新启动该服务。但它们确实具有该机器的正常用户访问(读取)权限。因此,我正在使用模拟。不幸的是,该应用程序仅适用于某些用户,而其他用户则失败。
所有这些用户属于同一个AD组,所有用户都拥有所运行计算机的本地管理员权限,并且已激活UAC。
以下是代码:
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
static void Main(string[] args)
{
SafeTokenHandle safeTokenHandle;
try
{
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_SERVICE = 5;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(user, domain, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeTokenHandle);
// Use the token handle returned by LogonUser.
using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
{
//Duplicate token to set Impersonation Level to Delegate (3)
IntPtr tokenDuplicate = IntPtr.Zero;
DuplicateToken(newId.Token, 3, ref tokenDuplicate);
using (WindowsIdentity newId2 = new WindowsIdentity(tokenDuplicate))
{
using (WindowsImpersonationContext impersonatedUser = newId2.Impersonate())
{
var token = newId2.ImpersonationLevel;
Console.WriteLine("Running as: " + WindowsIdentity.GetCurrent().Name);
Console.WriteLine("Impersonation level: " + token.ToString());
var sc = new ServiceController
{
MachineName = remote,
ServiceName = myService
};
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
while (!sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Refresh();
System.Threading.Thread.Sleep(100);
}
}
sc.Refresh();
if (sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Start();
while (!sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Refresh();
System.Threading.Thread.Sleep(100);
}
}
}
}
}
}
}
public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeTokenHandle()
: base(true)
{
}
[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);
protected override bool ReleaseHandle()
{
return CloseHandle(handle);
}
}
我使用过DuplicateToken,因为没有它,ImpersonationLevel是"无"所以我认为这可能是原因。唉它并没有改变一件事。
模拟上下文中使用的用户凭据在远程计算机上具有本地管理员权限,可以重新启动该服务。在使用此应用程序的六个用户中,其中4个服务正常重新启动,而另外两个用户则
失败无法在[remote]上启动服务控制管理器。你可能有 权利不足......
运行应用程序时的控制台输出适用于所有用户:
运行方式:[正确用户]
模仿级别:委派
以管理员身份运行应用程序也不会改变任何内容。 它是一个普通的独立控制台应用程序,而不是WCF服务或任何依赖IIS的东西。
所有用户都在域内运行Windows 7 Professional,直接连接到网络,而不是通过VPN。
任何想法可能是什么原因造成的?我错过了什么吗?失败的用户可能会遗漏一些东西吗?框架版本?再分发?什么?
答案 0 :(得分:0)
让它工作,虽然不是我真正喜欢的方式(因为它没有告诉我为什么其他代码对这两个用户失败)。
=>使用PowerShell的强大功能:
using System.Management.Automation;
var command = new PSCommand();
command.AddScript("$pass = convertto-securestring \"thePassword" -asplaintext -force");
command.AddScript("$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist \"DOMAIN\\user\",$pass");
command.AddScript("$obj = (gwmi -computername \"srv-inet\" -class Win32_Service -credential $mycred | Where-Object { $_.Name -match \"Name of the service\" })");
command.AddScript("$obj.StopService()");
using (var ps = PowerShell.Create())
{
ps.Commands = command;
ps.Invoke();
if (ps.HadErrors)
{
Console.WriteLine(ps.InvocationStateInfo.Reason);
}
while (ps.InvocationStateInfo.State != PSInvocationState.Completed)
System.Threading.Thread.Sleep(10);
//Script completed but service not yet stopped.
//Wait 5 seconds, show countdown to user
for (var i = 10; i > 5; i--)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
}
ps.Commands.Clear();
command = new PSCommand();
command.AddScript("$pass = convertto-securestring \"thePassword\" -asplaintext -force");
command.AddScript("$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist \"DOMAIN\\user\",$pass");
command.AddScript("$obj = (gwmi -computername \"srv-inet\" -class Win32_Service -credential $mycred | Where-Object { $_.Name -match \"Name of the service\" })");
command.AddScript("$obj.StartService()");
ps.Commands = command;
ps.Invoke();
if (ps.HadErrors)
{
Console.WriteLine(ps.InvocationStateInfo.Reason);
}
while (ps.InvocationStateInfo.State != PSInvocationState.Completed)
System.Threading.Thread.Sleep(10);
//Script completed but service not yet fully started.
//Wait 5 seconds, show countdown to user
for (var i = 5; i > 0; i--)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
}
}
这没有优化,你可以看到;我正在重复自己的服务开始。
但它有效,因此我很高兴。
仍然:如果有人知道什么可能导致ServiceController的初始代码对某些用户失败,请分享您的知识!
<强>附录:强> 即使PowerShell方法最初也不起作用;我不得不在其中一台发生故障的计算机上下载 Windows Management Framework 4.0 。这似乎加强了两台机器上确实缺少一些组件的情况。唉,我不知道,哪个。