使用PowerShell运行单个exe文件时出现异常。
- 计算机:Windows 10(最新补丁)
- PowerShell软件开发工具包:7.2.4
- 适用范围:控制台网络6.0
代码:
using System.Management.Automation;
Console.WriteLine("Test PowerShell Runner!");
var psInstance = PowerShell.Create();
psInstance.AddScript("(Get-Host).Name");
Console.WriteLine("Before Invoke.");
var returnObj = psInstance.Invoke();
Console.WriteLine("After Invoke.");
Console.WriteLine("Name: " + returnObj.First().BaseObject);
psInstance.Runspace?.Close();
Console.WriteLine("Was successful run");
项目文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.4" />
</ItemGroup>
</Project>
发布配置文件(Rider):[骑手个人资料设置-可在VS中复制][1][1]:www.example.comhttps://i.stack.imgur.com/dZFc0.png
由于安全原因,我们不能使用IncludeAllContentForSelfExtract(它将成功运行),我们希望尽可能隐藏所有代码。
还要注意的是,这将通过IDE在debug/run中成功运行,只有发布的版本会失败。
我们已经尝试在发布的exe路径中包含powershell dll,但没有影响。
错误:
C:\Projects\PSConsoleRunner\PSConsoleRunner\bin\Release\net6.0\publish>PSConsoleRunner.exe
Test PowerShell Runner!
Before Invoke.
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 Program.<Main>$(String[] args) in C:\Projects\PSConsoleRunner\PSConsoleRunner\Program.cs:line 10
2条答案
按热度按时间juud5qan1#
错误输出似乎是AddScript方法需要一个路径,所以我猜是这一行:
大概应该是:
示例来自以下文档:
https://learn.microsoft.com/en-us/powershell/scripting/developer/hosting/adding-and-invoking-commands?view=powershell-7.2#addcommand
pod7payv2#
PowerShell当前不支持作为单个文件发布。此处提供有关此内容的详细信息:#13540和解决方法:编号13540(备注)
此外,如果您想要一个自包含的应用,则需要提供不可移植的RID:#18225