I'm a .net developer working on a windows service and I'm experiencing a problem and was hoping someone could provide help. Detailed below is what we are working with.
The error is thrown on script.Invoke. For context, here is the method that calls it:
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()
Also, if anyone knows what Parameter 'Path1' is, if anyone has any recommendations, I'd love to hear them
Everything listed above and worth noting that we were publishing with the settings
We expected the same results as were on the local device. Our script executes with no errors.
Sounds like the "Self Contained" is the issue please see: https://github.com/PowerShell/PowerShell/issues/13540