2017-07-28 28 views
1

我想创建一个Gui的PowerShell脚本,给用户一个可用驱动器的下拉列表,然后当用户选择驱动器,然后单击确定脚本映射驱动器为M:\ 但我不能工作,如何获得在组合框中选择的项目要传递到变量$ 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 

我也想隐藏PowerShell窗口,但不是贵我试图隐藏-window但隐藏了一切

回答

0

ComboBox有几个事件,你可以绑定到那将做各种事情。其中一个事件是SelectedIndexChanged。您可以将该事件添加到您的ComboBox对象并更新$MapDrive

此代码$MapDrive = convert.ToString($Dropdown.SelectedItem)将在初始编译期间仅触发一次。在运行时编译后,必须使用事件触发代码更改。

此外,在Powershell中,您可以使用以下命令[System.Windows.Forms.ComboBox] | gm来获取对象的方法和属性列表。您可以使用[System.Windows.Forms.checkedlistbox].GetEvents()来获取对象的事件列表。

+0

这工作完全谢谢你我一直在谷歌搜索一个星期大声笑:) – Andy