I need to produce a command line app in a single exe file. It involves Microsoft.PowerShell.SDK package. I create a PowerShell object add commands and then use PowerShell.Invoke() method. On 7.0.0SDK it produces this error:
Unhandled exception. System.TypeInitializationException: The type initializer for 'System.Management.Automation.PSVersionInfo' threw an exception.
---> System.ArgumentException: The path is empty. (Parameter 'path')
at System.IO.Path.GetFullPath(String path)
at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
at System.Management.Automation.PSVersionInfo..cctor()
--- End of inner exception stack trace ---
at System.Management.Automation.PSVersionInfo.get_GitCommitId()
at Microsoft.PowerShell.Telemetry.ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry(String mode)
at Microsoft.PowerShell.Telemetry.ApplicationInsightsTelemetry.SendTelemetryMetric(TelemetryType metricId, String data)
at System.Management.Automation.PowerShell..ctor(PSCommand command, Collection`1 extraCommands, Object rsConnection)
at System.Management.Automation.PowerShell.Create()
at LanguageStickRemover.SettingsReader.GitConnector(String commandType, String pathToRepo)
at LanguageStickRemover.SettingsReader.ReadSettings()
at LanguageStickRemover.Program.Main(String[] args)
On 7.2.6SDK error looks a little different:
Unhandled exception. 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()
at LanguageStickRemover.SettingsReader.GitConnector(String commandType, String pathToRepo)
at LanguageStickRemover.SettingsReader.ReadSettings()
at LanguageStickRemover.Program.Main(String[] args)
It works on debug. It doesn't work on single file release so it's not a problem with a script. I use .NET6 and a latest Powershell.SDK release. Has anyone found a solution to this problem? I invoke like this:
private static void GitConnector(string commandType, string pathToRepo)
{
using (PowerShell ps = PowerShell.Create())
{
ps.AddCommand("param([string]$command, [string]$pathToRepo)")
.AddCommand("if($command -eq \"clone\")")
.AddCommand("{git clone $pathToRepo}")
.AddCommand("if($command -eq \"pull\")")
.AddCommand("{git pull}")
.AddParameter("command", commandType)
.AddParameter("pathToRepo", pathToRepo)
.Invoke();
}
}