我有一个控制台应用程序项目,我在其中使用 powershell SDK,它在调试中工作得很好,但在发布时,除了 powershell 之外的所有内容都可以工作。
我发现它起作用的唯一设置是在将项目发布为依赖于框架和可移植的项目时。或者至少它可以在我的电脑上运行,在其他计算机上它说 dotnet 运行时丢失,即使通过提供的链接安装也是如此。
使用独立的.net6-windows 和 win-x86 不起作用。不太确定可能出了什么问题?我尝试清理项目、解决方案、重新启动 Visual Studio 和我的电脑。在调试中一切都按预期工作,但是当我发布时,powershell 就不起作用了。
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-SocketService-04A4D71F-C305-4A5B-BD0B-529C28B25DAD</UserSecretsId>
<PlatformTarget>AnyCPU</PlatformTarget>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.1" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="System.Management.Automation" Version="7.2.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Speedtest\" />
</ItemGroup>
<ItemGroup>
<None Update="Speedtest\speedtest.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
EDIT:
我更改了我的 powershell 处理程序以使用 .EndInvoke(),现在我终于得到了一个正确的错误!我发现了一个提到错误的问题https://github.com/PowerShell/PowerShell/issues/7909 https://github.com/PowerShell/PowerShell/issues/7909但安装 Microsoft.Management.Infrastruct 并没有解决问题,我安装了版本 2.0.0。如果我尝试安装版本 1,则会收到一条错误消息,指出 System.Management.Automation 包需要更高版本 (2.0.0)
public static string CMD(string script)
{
string errorMsg = "";
string output;
ps.AddScript(script);
//Make sure return values are outputted to the stream captured by C#
ps.AddCommand("Out-String");
PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
ps.Streams.Error.DataAdded += (object sender, DataAddedEventArgs e) =>
{ errorMsg = ((PSDataCollection<ErrorRecord>)sender)[e.Index].ToString(); };
IAsyncResult result = ps.BeginInvoke<PSObject, PSObject>(null, outputCollection);
ps.EndInvoke(result);
//while (!result.IsCompleted)
// Thread.Sleep(300);
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject outputItem in outputCollection)
{
stringBuilder.AppendLine(outputItem.BaseObject.ToString());
}
output = stringBuilder.ToString();
//Clears commands added to runspace
ps.Commands.Clear();
if (!string.IsNullOrEmpty(errorMsg))
return string.Empty;
return output.Trim();
}
这是错误消息
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Den angivne fil blev ikke fundet.
File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult)
at SocketService.PowerShellHandler.CMD(String script) in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Handlers\PowershellHandler.cs:line 35
at SocketService.Classes.SystemInfo..ctor() in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Classes\SystemInfo.cs:line 21
at SocketService.CommandHandler.Initialize() in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Handlers\CommandHandler.cs:line 31
at SocketService.SocketService..ctor() in C:\Users\Blue\source\repos\SocketClientProject\Clientside\SocketService.cs:line 27
at SocketService.WindowsBackgroundService.StartAsync(CancellationToken cancellationToken) in C:\Users\Blue\source\repos\SocketClientProject\Clientside\WindowsBackgroundService.cs:line 17
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Program.<Main>$(String[] args) in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Program.cs:line 14
at Program.<Main>(String[] args)
[process exited with code 3762504530]
第 14 行是我调用 PowerShell.Create();