2016-04-24 4 views
0

私はpowershellでアプリケーションを作成しましたが、今はアプリケーションにGUIを追加する必要があります。 または書き込み textbokにフォルダのパスを選択するには、このアプリケーションで フォルダーを選択してテキストボックスに出力するpowershell

は、私は、ユーザーに機会を与える必要があります。私はこのコードを書いたが、私が作成したtexboxの get-FolderLocation関数を呼び出すことの成果を得ることはできなかった。

enter image description here

これを実現する方法についての任意のアイデア?

[void][System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) 
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.VisualBasic”) 

#####Define the form size & placement 

$form = New-Object “System.Windows.Forms.Form”; 
$form.Width = 500; 
$form.Height = 150; 
$form.Text = $title; 
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen; 

##############Define text label1 
$textLabel1 = New-Object “System.Windows.Forms.Label”; 
$textLabel1.Left = 25; 
$textLabel1.Top = 15; 
$textLabel1.Text = "select the folder"; 

############Define text box1 for input 
$textBox1 = New-Object “System.Windows.Forms.TextBox”; 
$textBox1.Left = 150; 
$textBox1.Top = 10; 
$textBox1.width = 200; 
$textBox1.Text = "selected folder" 

#############define select button 
$button = New-Object “System.Windows.Forms.Button”; 
$button.Left = 360; 
$button.Top = 85; 
$button.Width = 100; 
$button.Text = “Browse”; 

############# the output of calling the get-Folder Location function must be shown in the textbox1 

$button.Add_Click({get-Folderlocation}) ; 

#############Add controls to all the above objects defined 
$form.Controls.Add($button); 
$form.Controls.Add($textLabel1); 
$form.Controls.Add($textBox1); 

$form.ShowDialog(); 
$textBox1.Text 



$selectedDirectory 


function get-Folderlocation([string]$Message, [string]$InitialDirectory, [switch]$NoNewFolderButton) 
{ 
    $browseForFolderOptions = 0 
    if ($NoNewFolderButton) { $browseForFolderOptions += 512 } 

    $app = New-Object -ComObject Shell.Application 
    $folder = $app.BrowseForFolder(0, $Message, $browseForFolderOptions, $InitialDirectory) 
    if ($folder) { $selectedDirectory = $folder.Self.Path } else { $selectedDirectory = '' } 
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($app) > $null 
    return $selectedDirectory 
} 

答えて

1

あなたは、単にget-FolderLocationから$textBox1.Textに出力を割り当てます。

$textBox1変数がadd_Click()スクリプトブロックと同じスコープではないため、PowerShell 3.0および4.0では問題が解決されます。それはPowerShellの新しいバージョンでは完璧に動作しますが、私はPowerShellの2.0で試してみましたが、作業をしてthaaanks

$button.Add_Click({(Get-Variable -Name textBox1 -Scope 1).Value.Text = Get-Folderlocation}) 
+0

:この問題を回避するためにGet-Variable -Scope 1を使用してください。 PowerShellで取得したエラーです.2 Get-Folderlocationがコマンドレットとして認識されていません。 –

+0

@kimopryvtは関数定義をup * before * '$ form.ShowDialog()'の前に移動します。 –

+0

私は関数を動かしました。しかし、私はこのエラーが発生フォルダを選択します。 "スコープ番号 '1'がアクティブスコープの数を超えています。 –

関連する問題