C# 以本地管理员身份运行powershell脚本。为什么没有';不行?

C# 以本地管理员身份运行powershell脚本。为什么没有';不行?,c#,sharepoint,powershell,C#,Sharepoint,Powershell,我正在尝试以本地管理员身份运行此Powershell脚本($credential也具有PSSHELLAMIN权限): 它接着说: Connecting to remote server failed with the following error message : WinRM cannot process the request. The following error occured while using Kerb eros authentication: There are curr

我正在尝试以本地管理员身份运行此Powershell脚本(
$credential
也具有PSSHELLAMIN权限):

它接着说:

Connecting to remote server failed with the following error message :
 WinRM cannot process the request. The following error occured while using Kerb
eros authentication: There are currently no logon servers available to service
the logon request.
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specifie
d.
  -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 tru
st 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 H
elp topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken

我试图在PS->
Enable PSRemoting
中运行命令来解决此问题。但这对我没有帮助。那么,如何处理此错误呢?

看起来您正在尝试使用本地帐户建立网络连接“\test”。

希望这能帮助正在执行类似操作的人,但使用C代码

如果代码以localSystem运行(如窗口服务案例中),则在用户名前面不带“\”的情况下使用PSCredential打开运行空间似乎会强制工作组环境中的Kerberos AuthenticationMechanism

修复方法是在用户名前面添加“\”以强制在非域环境中使用NTLM

var username = "foobar";
var domain = "";
var connInfo = new WSManConnectionInfo(false, hostname, Port, AppName, ShellUri,
                new PSCredential(domain + "\\" + username, password))
            {
                AuthenticationMechanism = AuthenticationMechanism.Negotiate
            };

using (var runspace = RunspaceFactory.CreateRunspace(host, connectionInfo))
{
    runspace.Open(); //will try to negotiate with Kerberos in domain, NTLM in non-domain
}

需要更多环境信息:源/目标计算机是什么操作系统?有防火墙介入吗?您是否在源计算机和目标计算机上运行了
启用PSRemoting
?OS-Win7。本地防火墙已禁用(仅在本测试期间)。我确实在本地运行了它。我如何在本地机器上测试它?你能告诉我它是如何工作的吗?我试图在本地测试此脚本,但它不起作用。例如“localhost\test”?因此,我运行了newpssession命令,脚本在本地工作,但在服务器上有一个错误。创建新会话是一个好的决定吗?
Invoke命令将自动创建和销毁远程处理会话。除非您愿意,否则您不必手动执行此操作。
var username = "foobar";
var connInfo = new WSManConnectionInfo(false, hostname, Port, AppName, ShellUri,
                new PSCredential(username, password))
            {
                AuthenticationMechanism = AuthenticationMechanism.Negotiate
            };

using (var runspace = RunspaceFactory.CreateRunspace(host, connectionInfo))
{
    runspace.Open(); //will try to negotiate with Kerberos
}
var username = "foobar";
var domain = "";
var connInfo = new WSManConnectionInfo(false, hostname, Port, AppName, ShellUri,
                new PSCredential(domain + "\\" + username, password))
            {
                AuthenticationMechanism = AuthenticationMechanism.Negotiate
            };

using (var runspace = RunspaceFactory.CreateRunspace(host, connectionInfo))
{
    runspace.Open(); //will try to negotiate with Kerberos in domain, NTLM in non-domain
}