2014-09-10 120 views
0

我尝试使用具有目录名称的按钮创建动态表单。点击按钮应该取目录名并处理它。Powershell:创建动态表单

代码sinpped:

{function convert_it($arg) 

    $dir = "$source\$arg" 
    $dir_liste = Get-ChildItem $dir | Where-Object {$_.mode -match "d"} 
    $dir_count_total = $dir_liste.count 

... 

... 

... 

} 



foreach ($dir in $dir_list) 

{ 
# Button 
if ($split_count -eq 25){ 
    $x=250 
    $y=50 
} 
elseif ($split_count -eq 50){ 
    $x=500 
    $y=50 
} 

$dir_numbers = (get-childitem -Path $source\$dir -recurse | where-object { 
$_.PSIsContainer }).Count 

$run = New-Object System.Windows.Forms.Button 
$run.Location = New-Object System.Drawing.Size($x,$y) 
$run.Size = New-Object System.Drawing.Size(100,20) 
if ($dir_numbers -eq 0) { 
    $run.Enabled = $false 
} 
$run.Text = "#$dir_count -> $dir -> $dir_numbers" 

$run.Add_Click({ convert_it($dir) }.GetNewClosure()) 

$testForm.Controls.Add($run) 
$Font = New-Object System.Drawing.Font("Times New Roman",14, 
[System.Drawing.FontStyle]::Regular) 
$run.font = $Font 
$run.AutoSize = $True 
$y+=30 
$split_count+=1 
$dir_count+=1 

} 







$testForm.ShowDialog() 

当我运行该脚本,我得到以下错误:

convert_it:术语“convert_it未被识别为cmdlet,函数,脚本的名称文件或可操作程序。检查名称的拼写,或者如果包含路径 ,请验证路径是否正确,然后重试。

  • $ run.Add_Click({convert_it($ DIR)} .GetNewClosure())
  • ~~~~~~~~~~
    • CategoryInfo:ObjectNotFound:(convert_it:字符串) [],CommandNotFoundException
    • FullyQualifiedErrorId:CommandNotFoundException

什么我错在这里?:

$run.Add_Click({ convert_it $dir }.GetNewClosure()) 

回答

0

它看起来像你的功能在错误位置的开头括号(应该在行的末尾而不是在开头(这使它成为一个scriptblock代替))。

应该是这样的:

function convert_it($arg) { 

    $dir = "$source\$arg" 
    $dir_liste = Get-ChildItem $dir | Where-Object {$_.mode -match "d"} 
    $dir_count_total = $dir_liste.count 
    ... 
}