我是一名从事 Windows 服务工作的 .net 开发人员,我遇到了问题,希望有人可以提供帮助。下面详细介绍了我们正在合作的内容。
script.Invoke 上抛出错误。对于上下文,这是调用它的方法:
private void RunPcsScript(string pcsServiceName, string newestVersion, string pcsPath)
{
_logger.Info("Creating Powershell");
using var ps = PowerShell.Create();
if (!string.IsNullOrEmpty(pcsServiceName))
{
_logger.Info("Removing old PCS Service");
IDictionary removeParams = new Dictionary<String, String>();
removeParams.Add("Name", pcsServiceName);
ps.AddCommand("Remove-Service").AddParameters(removeParams).Invoke();
}
_logger.Info("Creating new PCS Service");
IDictionary parameters = new Dictionary<String, String>();
parameters.Add("BinaryPathName", $"{pcsPath}\\SomeExecutable.exe");
parameters.Add("DisplayName", "MyServiceName");
parameters.Add("Name", $"MyServiceName-{newestVersion}");
var script = ps.AddCommand("New-Service").AddParameters(parameters);
var results = script.Invoke();
}
System.TypeInitializationException: The type initializer for 'System.Management.Automation.ExperimentalFeature' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Management.Automation.Configuration.PowerShellConfig' threw an exception.
---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
at System.IO.Path.Combine(String path1, String path2)
at System.Management.Automation.Configuration.PowerShellConfig..ctor()
at System.Management.Automation.Configuration.PowerShellConfig..cctor()
--- End of inner exception stack trace ---
at System.Management.Automation.ExperimentalFeature..cctor()
--- End of inner exception stack trace ---
at System.Management.Automation.Runspaces.InitialSessionState.AddVariables(IEnumerable`1 variables)
at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke()
另外,如果有人知道参数“Path1”是什么,如果有人有任何建议,我很想听听他们
上面列出的所有内容以及值得注意的是我们正在使用设置进行发布
我们期望与本地设备上的结果相同。我们的脚本执行时没有错误。
听起来“自包含”是问题所在,请参阅:https://github.com/PowerShell/PowerShell/issues/13540