Powershell未将c#代码编译为.exe

Powershell未将c#代码编译为.exe,c#,.net,powershell,C#,.net,Powershell,我有一些代码没有在PowerShell中编译成.exe文件 $csharp = '#CSharp code goes here' $tmpFile = [IO.Path]::GetTempFileName() + ".cs" # Creates Temp file Out-file -FilePath $tmpFile -InputObject $csharp # sets content Start-Process -FilePath C:\Windows\Microsoft.

我有一些代码没有在PowerShell中编译成.exe文件

  $csharp = '#CSharp code goes here'
  $tmpFile = [IO.Path]::GetTempFileName() + ".cs" # Creates Temp file
  Out-file -FilePath $tmpFile -InputObject $csharp # sets content
  Start-Process -FilePath C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe -ArgumentList "/out:Launcher.exe" # Starts csc
加载脚本时,会出现一个带有csc.exe的弹出窗口,该窗口在毫秒内关闭。
有人能帮忙吗?谢谢,CollinScripter

要防止出现新窗口,您可以使用
nonewindow
开关:

Start-Process -FilePath C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe -NoNewWindow -ArgumentList "/out:Launcher.exe"

注意,当前命令没有传递源文件进行编译。

添加输入后效果更好。我生成了一个
致命错误CS2008:没有为您的代码片段指定输入

  $csharp = '#CSharp code goes here'
  $tmpFile = [IO.Path]::GetTempFileName() + ".cs" # Creates Temp file
  Out-file -FilePath $tmpFile -InputObject $csharp # sets content

Start-Process -FilePath C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe -ArgumentList "/addmodule:$tmpFile /out:Launcher.exe" # Starts csc

我看不出您是如何将tmp文件指定到csc命令中的,您只是希望得到一个没有输入的输出。还要添加bugreport以查看错误<代码>“/addmodule:$tmpFile/out:Launcher.exe bugreport:c:\WhyItFailed.txt
尝试一下,然后返回报告现在我收到一个损坏的文件错误。致命错误CS0009:无法打开元数据文件“c:\Users\CollinScripter\AppData\Local\Temp\tmp1795.cs”——“文件已损坏。”可能在$csharp变量中,是否确定它是一个功能完整的c应用程序。.cs temp文件是否与所需的输出相匹配(用notepad.exe打开)
启动进程-文件路径C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe-ArgumentList”/out:Launcher.exe$tmpFile“#启动csc
如果这能解决问题,我将编辑答案.C:\Users\CollinScripter\AppData\Local\temp\tmpD546.cs(2,13):错误C S0116:命名空间不能直接包含字段或方法等成员C:\Users\CollinScripter\AppData\Local\Temp\tmpD546.cs(3,24):错误CS1518:预期的类、委托、枚举、接口或结构不知道如何执行此操作。