Pass a c# Object Value To Powershell as Variable

206 Views Asked by At

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

1

There are 1 best solutions below

10
Mathias R. Jessen On

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