当我从命令行执行 powershell 脚本时,我得到了预期的 csv 文件输出,但是当我从 C# 执行时,没有生成 csv 输出文件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace POC_UI_PWSExecution
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnExecPWS001_Click(object sender, EventArgs e)
{
var ps1File = @"C:\project_source\pwsSandbox\test.ps1";
var startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy ByPass -File \"{ps1File}\"",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(startInfo);
}
}
}
`Get-Process | Export-Csv -Path processes.csv`
我已经从命令行测试了 powershell 脚本,它完美地工作,创建了 csv 文件。