【问题标题】:PowerShell remoting from a Windows service从 Windows 服务进行 PowerShell 远程处理
【发布时间】:2013-11-07 01:04:37
【问题描述】:

我有一个 Windows 服务,它通过WsManConnectionInfo/RunspaceFactory 定期在远程计算机上运行 PowerShell 脚本(按照本文中的步骤:Remotely Executing Commands in PowerShell using C#):

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 中列出)。

编辑:更多信息(cmets 总结):

  • 本地计算机:Windows 7 Ultimate 64bit(Windows 8 机器上的虚拟机)。
  • 远程计算机:Windows Server 2008R2 Datacenter 64bit。
  • 我们不想更改服务用户帐户的主要原因是,这是对已部署在许多客户端(客户)上的旧服务的更新。
  • 该服务还访问 Windows 注册表和本地计算机上的文件系统,因此将用户帐户设置为更多限制,例如 NetworkService,只会打开不同的蠕虫罐。李>

【问题讨论】:

  • 您在哪个版本的 Windows 上运行它?
  • @mjolinor - 本地计算机:Windows 7 Ultimate 64bit(Windows 8 机器上的虚拟机)。远程计算机:Windows Server 2008R2 Datacenter 64bit。
  • 您是否尝试过使用 NetworkService 帐户而不是 LocalSystem 帐户?
  • @mjolinor - 感谢您的宝贵时间!该服务还读取/写入 Windows 注册表,所以如果我使用 NetworkService,我会在(重新)启动服务时得到一个UnauthorizedAccessException..
  • 您应该能够在注册表权限中修复它。

标签: c# powershell


【解决方案1】:

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

我不知道为什么只有在连接 LocalSystem 帐户时才需要前缀...

【讨论】:

  • 谢谢。几天来,我一直在努力解决完全相同的问题,这很有帮助。您是否偶然发现了解决方案,或者是否有此信息的来源?
  • @Cozzamara - 这只是一个同事的建议,所以恐怕我没有更多信息给你:)
  • 感谢您的解决方案,我也可以在 MYREMOTESERVERNAME 中添加,我尝试了计算机名称,但没有成功,但后来我尝试了 MYREMOTESERVERIPADDRESS\remoteusername,它成功了。
  • 我在 PowerShell 脚本中运行“Invoke-Command”时遇到了这个问题(直接在 CLI 上运行良好)。我发现我只需要添加反斜杠,例如\remoteusername。感谢您的修复!
  • 我使用的是.\remoteusername,它看起来稍微熟悉一些。无论如何,感谢您的精彩提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 2010-09-24
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 2018-09-15
相关资源
最近更新 更多