gpt4 book ai didi

c# - 从已发布的单个 Exe 本地运行的 PowerShell 失败(没有 IncludeAllContentForSelfExtract)

转载 作者:行者123 更新时间:2023-12-05 05:39:49 30 4
gpt4 key购买 nike

使用 PowerShell 运行单个 exe 文件时出现异常。

  • 计算机:Windows 10(最新补丁)
  • PowerShell SDK:7.2.4
  • 应用:控制台net6.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>

发布个人资料(骑手):[骑手的个人资料设置-可以在VS中复制][1][1]: /image/dZFc0.png

出于安全原因,我们不能使用 IncludeAllContentForSelfExtract(它将成功运行),我们希望尽可能隐藏所有代码。

还要注意,这将通过您的 IDE 在调试/运行中成功运行,只有发布的版本失败。

我们已经尝试在发布的 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

最佳答案

错误输出似乎是 AddScript 方法需要路径,所以我猜是这一行:

psInstance.AddScript("(Get-Host).Name");

应该是:

psInstance.AddCommand("(Get-Host).Name");

此处文档中的示例:

https://learn.microsoft.com/en-us/powershell/scripting/developer/hosting/adding-and-invoking-commands?view=powershell-7.2#addcommand

关于c# - 从已发布的单个 Exe 本地运行的 PowerShell 失败(没有 IncludeAllContentForSelfExtract),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72579618/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com