【问题标题】:Pass a c# Object Value To Powershell as Variable将 c# 对象值作为变量传递给 Powershell
【发布时间】:2022-01-07 08:11:24
【问题描述】:

以下代码包含 2 个按钮,导致文件夹浏览器对话框(1,2),结果 1 应该作为 $source 传递,第二个作为 $destination。最后一个按钮应该是运行 PS 脚本

        private void guna2Button1_Click_1(object sender, EventArgs e)
    {
      
        
            DialogResult auswahl1 = folderBrowserDialog1.ShowDialog();
            if (auswahl1 == DialogResult.OK)
            {
                guna2TextBox4.Text = folderBrowserDialog1.SelectedPath;
            }
        
    }

    private void guna2Button2_Click_1(object sender, EventArgs e)
    {
        
        
            DialogResult auswahl2 = folderBrowserDialog2.ShowDialog();
            if (auswahl2 == DialogResult.OK)
            {
                guna2TextBox5.Text = folderBrowserDialog2.SelectedPath;
            }

    }


        //#####################################ZUORDNEN##############################


    private void materialButton3_Click(object sender, EventArgs e)
    {
        using (PowerShell ps = PowerShell.Create())
        {   
            ps.AddParameter("Source", folderBrowserDialog1.SelectedPath);
            ps.AddParameter("Destination", folderBrowserDialog2.SelectedPath);
            ps.AddCommand("zuordnung.ps1");

        }
    }

zuordnung.ps1 包括

Get-ChildItem $Source | ForEach-Object {
$Tail = ($_.BaseName -split '_')[-2]
$Filter = "*_{0}" -f $Tail
$DestDir = Get-ChildItem $Destination -Filter $Filter -Directory | Select-Object -ExpandProperty FullName -First 1
if ($DestDir) {
    Copy-Item $_.FullName $DestDir -Force
} else {
    "No Directory was found that matched the Filter {0} in Directory {1}" -f $Filter, $Destination | Write-Host
} }

我已将 ps 脚本更改为

param( 
$Source,
$Destination
)
    
Get-ChildItem $Source | ForEach-Object {
    $Tail = ($_.BaseName -split '_')[-2]
    $Filter = "*_{0}" -f $Tail
    $DestDir = Get-ChildItem $Destination -Filter $Filter -Directory | Select-Object -ExpandProperty FullName -First 1
    if ($DestDir) {
        Copy-Item $_.FullName $DestDir -Force
    } else {
        "No Directory was found that matched the Filter {0} in Directory {1}" -f $Filter, $Destination | Write-Host
    }
}

以及相关的 .net 位

  private void materialButton3_Click(object sender, EventArgs e)
    {
        using (PowerShell ps = PowerShell.Create())
        {
            ps.AddScript($@"C:\Users\User\Desktop\Tool_Release\Tool\bin\Debug\zuordnung.ps1");
            ps.AddParameter("Source", folderBrowserDialog1.SelectedPath);
            ps.AddParameter("Destination", folderBrowserDialog2.SelectedPath);
        }
    }

感谢Mathias 但似乎仍然无法在单击按钮时运行它。没有错误。 我错过了什么?

提前致谢

【问题讨论】:

  • 你试过-Path参数吗?
  • 您需要编辑问题并将错误代码与示例一起发布。通过Minimal-Reproducible-Example
  • 我已经编辑了这个问题。我将对 -path 进行一些研究。
  • 我认为问题很明确,如何在 PS 中将 folderBrowserDialog1.SelectedPath 的值传递给 $source

标签: c# powershell folderbrowserdialog


【解决方案1】:

param(...) 块添加到您的脚本中:

param(
  [string]$Source,
  [string]$Destination
)

Get-ChildItem $Source | ForEach-Object {
  # ...
}

然后将你的 C# 代码更改为调用 AddParameter你调用了 AddCommand:

ps.AddCommand("zuordnung.ps1");
ps.AddParameter("Source", folderBrowserDialog1.SelectedPath);
ps.AddParameter("Destination", folderBrowserDialog2.SelectedPath);

【讨论】:

  • @BlackArch.py​​ 不客气。如果我的回答解决了您的问题,请考虑通过单击左侧的复选标记将其标记为“已接受”:)
  • Destination*,进行了更改,但它仍然对我不起作用,ps 脚本与发布 exe 位于同一文件夹中。位置不对?
  • 它不是脚本,它适用于其中设置的源和目标
  • @BlackArch.py​​ 在调用后检查 ps.HadErrorsps.Streams.Errors 以查看可能发生的任何错误。如果它无法识别或找到zuordnung.ps1,请尝试提供根路径(例如C:\path\to\app\zuordnung.ps1 而不是zuordnung.ps1
  • 你在哪里运行脚本?我没有看到任何ps.Invoke();
猜你喜欢
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 2017-08-22
相关资源
最近更新 更多