从 C# 执行时,Powershell 脚本不创建 csv 文件

问题描述 投票:0回答:0

当我从命令行执行 powershell 脚本时,我得到了预期的 csv 文件输出,但是当我从 C# 执行时,没有生成 csv 输出文件。

C# 代码:(TestPWSScript)

    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);
            }
        }
    }

Powershell 脚本:(test.ps1)

`Get-Process | Export-Csv -Path processes.csv`

我已经从命令行测试了 powershell 脚本,它完美地工作,创建了 csv 文件。

c# powershell output export-to-csv
© www.soinside.com 2019 - 2024. All rights reserved.