0

我正在尝试将参数从 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"
4

2 回答 2

1

试试这个:

$run.Add_Click({ c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1 $($d.filename);})
于 2013-01-28T13:55:03.810 回答
1

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

$run.Add_Click({ 
    if($d.filename)
    {
        $path = $d.filename
        c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1 "'$path'"
    }
})
于 2013-01-28T13:58:58.693 回答