1

The following code includes 2 buttons resulting in a folderbrowserdialog(1,2), result of 1 is supposed to be passed as $source and the second as $destination. The last button is supposed to run the PS Script

        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 includes

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

i've changed the ps script to

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

and the relevant .net bit to

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

Thanks to Mathias but still dont seem to be able to run it on button click. no errors. What am i missing?

Thanks in Advance

  • have you tried `-Path` paramerter? – jazb Jan 07 '22 at 08:19
  • You need to edit the question and post the error code with sample. Go through [Minimal-Reproducible-Example](https://stackoverflow.com/help/minimal-reproducible-example#:~:text=Minimal%20and%20readable,correctly%20formatted%20on%20Stack%20Overflow.) – Ranadip Dutta Jan 07 '22 at 08:31
  • i've edited the question. i will take some research on -path. – BlackArch.py Jan 07 '22 at 09:28
  • i think the question is clear, how do i pass value of folderBrowserDialog1.SelectedPath to $source in PS – BlackArch.py Jan 07 '22 at 12:48

1 Answers1

0

Add a param(...) block to your script:

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

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

And change your C# code to call AddParameter after you've called AddCommand:

ps.AddCommand("zuordnung.ps1");
ps.AddParameter("Source", folderBrowserDialog1.SelectedPath);
ps.AddParameter("Destination", folderBrowserDialog2.SelectedPath);
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • @BlackArch.py You're most welcome. If my answer solves your problem, consider marking it "accepted" by clicking the checkmark on the left :) – Mathias R. Jessen Jan 07 '22 at 13:38
  • Destination*, made the changes but it still wont work for me, the ps script is lying in the same folder as the release exe. wrong loc? – BlackArch.py Jan 07 '22 at 13:40
  • its not the script tho, it works with source and destination set within – BlackArch.py Jan 07 '22 at 13:40
  • @BlackArch.py Inspect `ps.HadErrors` and `ps.Streams.Errors` after invocation to see any errors that might have occurred. If it can't recognize or find `zuordnung.ps1`, try supplying a rooted path (eg. `C:\path\to\app\zuordnung.ps1` instead of `zuordnung.ps1`) – Mathias R. Jessen Jan 07 '22 at 13:42
  • im gonna try this, thanks for ur time – BlackArch.py Jan 07 '22 at 13:43
  • i've made the mentioned changes, still does nothing. doesnt throw no errors either.. – BlackArch.py Jan 07 '22 at 13:57
  • could this be because of the way the folderbrowserdialog output (without quotations) ? – BlackArch.py Jan 07 '22 at 13:58
  • @BlackArch.py No, you don't need to worry about quoting - you're binding an already-existing string value to the parameter, so there's no literal text to be parsed. What happens if you read it from the text box you assigned the value to instead? `AddParameter("Source", guna2TextBox4.Text)` – Mathias R. Jessen Jan 07 '22 at 14:03
  • 1
    where do you run the script ? I don't see any `ps.Invoke();` – CFou Jan 07 '22 at 14:17
  • i've added 'ps.Invoke()' and tried replacing the dialog result with the textbox value still nothing. – BlackArch.py Jan 08 '22 at 12:46