【发布时间】:2020-02-06 12:26:19
【问题描述】:
我有一个用 C# 编写的 Windows 服务,它使用以下代码在其中调用一个 powershell 脚本:
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
using (var powershell = PowerShell.Create())
{
powershell.Runspace = runspace;
powershell.AddCommand("Invoke-Command")
.AddParameter("ComputerName", remoteComputer)
.AddParameter("FilePath", scriptPath)
.AddParameter("ArgumentList", new object[] { credential,IndexList,FilePath });
psOutput = powershell.Invoke();
}
runspace.Close();
}
我不断收到以下错误:
Connecting to remote server <computername> failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
虽然如果将其作为控制台应用程序运行,则相同的代码可以工作,但是在将其作为 Windows 服务运行时出现此错误。
我在这里做错了吗?还是有另一种方法可以在 Windows 服务中调用 powershell 脚本?
【问题讨论】:
-
windows sservice 在其下工作的用户没有访问权限来访问您尝试访问的任何内容。
-
Windows 服务正在以本地用户身份运行,这也是管理员,如果这是与权限相关的问题,为什么当它作为控制台应用程序运行时会起作用?
标签: c# powershell windows-services