2017-07-28 12 views
1

ユーザーに利用可能なドライブのドロップリストを提供するGuiを使用してPowerShellスクリプトを作成しようとしているときに、ユーザーがドライブを選択してOKをクリックすると、M:\ しかし、私はまた、PowerShellウィンドウではなく、私は-window隠さ試してみましたGUIを非表示にするが、その変数$ MapDrivepowershellのコンボボックスから変数へ

#List of Drives To Map 
$DriveList = @("0. DGL_Data","1. P1","2. DGLFSG3","3. p3 (TPC)","4. p4","6. p6","7. p7","8. p8","9. p9","10. p10","11. p11","12. p12") 
#Displays With Drive to Map 
$MapDrive = convert.ToString($Dropdown.SelectedItem) 

Function MapDrive { 

If ($MapDrive -eq $DriveList[0]){ 
Write-Output Sucess > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt"} 

ElseIf ($MapDrive -eq $DriveList[1]){ 
Write-Output BooYah > "C:\Users\andy.burton\Desktop\Practice Selector\Yes.txt" 
} 

Else { 
Write-Output Failure > "C:\Users\andy.burton\Desktop\Practice Selector\Failed.txt"} 
} 

#test Function 
Function test { 
Write-Output $MapDrive > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt" 
} 


#Function to Create Form 
Function GenerateForm { 

#Define Drive Selector Main Form 
Add-Type -AssemblyName System.Windows.Forms 
$DGL = New-Object system.Windows.Forms.Form 
$DGL.Text = "DGL Practice Manager" 
$DGL.TopMost = $true 
$DGL.BackgroundImage = [system.drawing.image]::FromFile("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical.jpg") 
$DGL.Icon = New-Object system.drawing.icon("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical2.ico") 
$DGL.Width = 600 
$DGL.Height = 265 
$DGL.MinimizeBox = $False 
$DGL.MaximizeBox = $False 

#Label to Display Instuctions 
$label2 = New-Object system.windows.Forms.Label 
$label2.Text = "Select which drive" 
$label2.BackColor = "#e4f3fa" 
$label2.AutoSize = $true 
$label2.Width = 25 
$label2.Height = 10 
$label2.location = new-object system.drawing.point(20,28) 
$label2.Font = "Microsoft Sans Serif,10" 
$DGL.controls.Add($label2) 

#Dropdown Box For Selecting Practice 
$Dropdown = New-Object system.windows.Forms.ComboBox 
$Dropdown.BackColor = "#e4f3fa" 
$DropDown.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList 
$Dropdown.Width = 243 
$Dropdown.Height = 20 
$Dropdown.location = new-object system.drawing.point(21,73) 
$Dropdown.Font = "Microsoft Sans Serif,10" 
$DropDown.items.addrange($DriveList) 
$DGL.controls.Add($Dropdown) 

#Cancel Button to Cancel drive Selection 
$Cancelbutton = New-Object system.windows.Forms.Button 
$Cancelbutton.Text = "Cancel" 
$Cancelbutton.Width = 60 
$Cancelbutton.Height = 30 
$Cancelbutton.location = new-object system.drawing.point(210,120) 
$Cancelbutton.Font = "Microsoft Sans Serif,10" 
$DGL.CancelButton = $Cancelbutton 
$CancelButton.Add_Click({ $DGL.close();[System.Windows.Forms.Application]::Exit($null)}) 
$DGL.controls.Add($Cancelbutton) 

#OK Button to Select Drive 
$OKbutton = New-Object system.windows.Forms.Button 
$OKbutton.Text = "OK" 
$OKbutton.Width = 60 
$OKbutton.Height = 30 
$OKbutton.location = new-object system.drawing.point(140,120) 
$OKbutton.Font = "Microsoft Sans Serif,10" 
$DGL.AcceptButton = $OKbutton 

$MapDrive = convert.ToString($Dropdown.SelectedItem) 
#On click call PracticeSelectedCallBack to launch the application 
$OKbutton.Add_Click({test ; $DGL.close()}) 
$DGL.controls.Add($OKbutton) 

    #Display the Form 
$DGL.Add_Shown({$DGL.Activate()}) 
$DGL.ShowDialog() 
} 


GenerateForm 

に渡されるコンボボックスで選択した項目を取得する方法をうまくカントすべてを隠してください

答えて

0

ComboBoxには、さまざまなことをするいくつかのイベントがあります。イベントの1つはSelectedIndexChangedです。 ComboBoxオブジェクトにそのイベントを追加して更新することができます$MapDrive

このコード$MapDrive = convert.ToString($Dropdown.SelectedItem)は、最初のコンパイル時に1回だけ起動します。実行時にコンパイルした後にイベントを使用してコード変更をトリガーする必要があります。

またPowershellでは、次のコマンド[System.Windows.Forms.ComboBox] | gmを使用して、メソッドのリストとオブジェクトのプロパティを取得できます。 [System.Windows.Forms.checkedlistbox].GetEvents()を使用すると、オブジェクトのイベントのリストを取得できます。

+0

これは完璧に働いてくれてありがとうございました。私は約1週間笑っていました:) – Andy

関連する問題