[英]Pass a c# Object Value To Powershell as Variable
The following code includes 2 buttons resulting in a folderbrowserdialog(1,2), 1 is supposed to be passed as $source and the second as $destination.以下代码包含 2 个按钮,导致文件夹浏览器对话框(1,2),1 应该作为 $source 传递,第二个作为 $destination。 The last button is supposed to run the PS Script最后一个按钮应该运行 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 includes 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
} }
i've changed the ps script to我已将 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
}
}
and the relevant .net bit to和相关的 .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);
}
}
Thanks to Mathias but still dont seem to be able to run it on button click.感谢Mathias ,但似乎仍然无法在单击按钮时运行它。 no errors.没有错误。 What am i missing?我错过了什么?
Thanks in Advance提前致谢
Add a param(...)
block to your script:在脚本中添加一个param(...)
块:
param(
[string]$Source,
[string]$Destination
)
Get-ChildItem $Source | ForEach-Object {
# ...
}
And change your C# code to call AddParameter
after you've called AddCommand
:并在调用 AddCommand后更改AddCommand
代码以调用AddParameter
:
ps.AddCommand("zuordnung.ps1");
ps.AddParameter("Source", folderBrowserDialog1.SelectedPath);
ps.AddParameter("Destination", folderBrowserDialog2.SelectedPath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.