从Windows服务进行PowerShell远程控制

17

我有一个Windows服务,定期通过WsManConnectionInfo/RunspaceFactory在远程计算机上运行PowerShell脚本(按照这篇文章的步骤执行:使用C#远程执行PowerShell命令):

var connectionInfo = new WSManConnectionInfo(false, server, 5985, "/wsman",
                                             "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
                                             cred)
                        {
                            OperationTimeout = 4*60*1000,
                            OpenTimeout = 1*60*1000
                        };
using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
{
    runSpace.Open();
    using (var p = runSpace.CreatePipeline())
    {
        p.Commands.AddScript(script);
        var output = p.Invoke();
        ...
    }
}

现在,如果我使用管理员账户运行Windows服务,一切正常。但是如果我使用LocalSystem账户运行服务,则会出现以下异常;

System.Management.Automation.Remoting.PSRemotingTransportException:
    Connecting to remote server NOSRVDEV02 failed with the following error message :
        WinRM cannot process the request. The following error with
        errorcode 0x8009030d occurred while using Negotiate authentication:
        A specified logon session does not exist. It may already have been terminated.

    Possible causes are:
        -The user name or password specified are invalid.
        -Kerberos is used when no authentication method and no user name are specified.
        -Kerberos accepts domain user names, but not local user names.
        -The Service Principal Name (SPN) for the remote computer name and port does not exist.
        -The client and remote computers are in different domains and there is no trust between the two domains.

    After checking for the above issues, try the following:
        -Check the Event Viewer for events related to authentication.
        -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.
         Note that computers in the TrustedHosts list might not be authenticated.
        -For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

    at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
    at System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.EndOpen(IAsyncResult asyncResult)
    at System.Management.Automation.RemoteRunspace.Open()
    ...

注意:这与 WSManConnectionInfo 的凭据无关——仅涉及服务属性中“登录”选项卡中的帐户设置。

我不想给予服务管理员权限。任何想法为什么LocalSystem用户无法登录?

附加信息:

  • 远程计算机不是域成员。
  • 我尝试使用IP地址和主机名进行连接(两者都在本地计算机的TrustedHosts中列出)。

编辑:更多信息(摘要注释):

  • 本地计算机:Windows 7 Ultimate 64位(在Windows 8框中的虚拟机)。
  • 远程计算机:Windows Server 2008R2 Datacenter 64位。
  • 我们不想更改服务用户帐户的主要原因是,这是对已经部署在许多客户端(客户)上的旧服务的更新。
  • 该服务还会访问本地计算机上的Windows注册表和文件系统,因此将用户帐户设置为像NetworkService这样的更受限制的东西只会带来不同的问题。

@mjolinor - 本地计算机:Windows 7 Ultimate 64位(在Windows 8虚拟机上)。远程计算机:Windows Server 2008R2 Datacenter 64位。 - Sphinxxx
你尝试过使用NetworkService账户而不是LocalSystem账户吗? - mjolinor
@mjolinor - 感谢您的时间!该服务还从/写入Windows注册表,因此如果我使用NetworkService在(重新)启动服务时会出现“UnauthorizedAccessException”。 - Sphinxxx
请注意,LocalSystem帐户拥有比您在此处指示或需要的更多特权。[来自MSDN](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684190(v=vs.85).aspx):LocalSystem帐户“在本地计算机上具有广泛的特权,并充当网络上的计算机。其令牌包括NT AUTHORITY \ SYSTEM和BUILTIN \ Administrators SIDs;这些帐户可以访问大多数系统对象。” - tnw
@mjolinor - 再次感谢您的帮助!您能详细说明一下远程系统上的权限需要如何更改吗? - Sphinxxx
显示剩余10条评论
1个回答

37

这个问题有一个相当令人惊讶的解决方案:在PSCredential对象(cred)中,用户名需要以无域名远程计算机的名称作为前缀,例如"MYREMOTESERVERNAME\remoteusername"而不是只有"remoteusername"。

虽然我不知道为什么连接LocalSystem帐户时需要使用前缀...


谢谢。我已经为同样的问题苦苦挣扎了好几天,这篇文章帮了我很多。你是偶然发现了解决方案,还是有什么信息来源呢? - Cozzamara
@Cozzamara - 这只是我的同事提出的一个建议,所以我恐怕没有更多的信息可以提供给你 :) - Sphinxxx
3
感谢您提供的解决方案。另外,我可以补充一点,在“MYREMOTESERVERNAME”中,我尝试使用计算机名称,但它没有起作用,但是当我尝试使用“MYREMOTESERVERIPADDRESS\remoteusername”的格式时,它起作用了。 - Bomberlt
7
当我在PowerShell脚本中运行“Invoke-Command”时(在CLI上直接运行正常),出现了这个问题。我发现我只需要添加反斜杠,例如\remoteusername。感谢修复! - KingBob
2
我正在使用 .\remoteusername,这看起来更加熟悉。无论如何,感谢您提供的绝佳提示! - CodeFox
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接