从Windows窗体文件浏览器将参数传递给powershell

时间:2013-01-28 13:46:26

标签: windows forms powershell

我试图将一个parmeter从windows窗体对话框搜索到另一个脚本,但似乎无法从用户选择文件时拉出参数。参数应该是用户想要安装的字体的完整文件位置。任何帮助将不胜感激,下面的脚本是格式和参数需要通过底部的脚本。

cls
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")    

#Windows form settings
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Icon = $Icon
$objForm.Text = "Font Installer"
$objForm.Size = New-Object System.Drawing.Size(350,350) 
$objForm.StartPosition = "CenterScreen"
$objForm.FormBorderStyle = "FixedDialog"
$objForm.BackgroundImage = $Image
$objForm.BackgroundImageLayout = "None"

#Browse for file
$d = New-Object Windows.Forms.OpenFileDialog
$d.initialDirectory = $initialDirectory
$d.filter = "All files (*.*)| *.*"
$d.ShowHelp = $true
$d.InitialDirectory = "c:\"
$d.Title = "Choose your Font"
$d.FileName
$d.filter = " Font Files (*.ttf; *.fon; *.fnt; *.ttc; *.otf; *.mmm; *.pbf; *.pfm)| *.ttf;     *.fon; *.fnt; *.ttc; *.otf; *.mmm; *.pbf; *.pfm"
#Browse Button
$button1 = New-Object system.Windows.Forms.Button
$button1.Text = "Select Font"
$button1.Add_Click({$d.ShowDialog( )})
$button1.Location = New-Object System.Drawing.Size(100,120)
$button1.Size = New-Object System.Drawing.Size(150,23)
$objForm.controls.add($button1)

#Install Button
$run = New-Object System.Windows.Forms.Button
$run.Location = New-Object System.Drawing.Size(100,170)
$run.Size = New-Object System.Drawing.Size(150,100)
$run.Text = "Install"
$Font1 = New-Object System.Drawing.Font("Arial Black",19,    [System.Drawing.FontStyle]::regular)
$run.Font = $Font1
$run.BackColor ="green"
#invoke expression - open install script and sent the parameter to it
$run.Add_Click({ 
Invoke-Expression "& `"c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1`"        $d.filename";



})
$objForm.Controls.Add($run)



$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

应该获取参数的脚本名为testparam.ps1,代码如下:

param(
    [string] $path = ""
)
Write-Host "this is a test the parameter is $path"

2 个答案:

答案 0 :(得分:1)

试试这个:

$run.Add_Click({ c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1 $($d.filename);})

答案 1 :(得分:1)

尝试将添加处理程序更改为:

$run.Add_Click({ 
    if($d.filename)
    {
        $path = $d.filename
        c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1 "'$path'"
    }
})