Powershell - 双下拉菜单选择

时间:2013-01-25 17:27:44

标签: powershell drop-down-menu

我正在使用我的一个脚本的下拉菜单,我调用它两次以在两个不同的窗口上获得两个不同的值。我想优化我的脚本并在同一个窗口中创建一个带有两个下拉框的窗口。

看看我的剧本:

    $DropDownArray = "Value1" , "Value2" , "Value3"


    # Form building

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 300
    $Form.height = 200
    $Form.Text = ”User move Original Location Box”
    $Form.StartPosition = "CenterScreen"


    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(100,10)
    $DropDown.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item in $DropDownArray) {
    $DropDown.Items.Add($Item) | Out-Null
    }

    $Form.Controls.Add($DropDown)

    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel.size = new-object System.Drawing.Size(100,200)
    $DropDownLabel.Text = "Select the user Original location ?"
    $Form.Controls.Add($DropDownLabel)


    $OKButton = new-object System.Windows.Forms.Button
    $OKButton.Location = new-object System.Drawing.Size(100,50)
    $OKButton.Size = new-object System.Drawing.Size(100,20)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({
    $Form.DialogResult = "OK"
    $Form.close()
    })
    $form.Controls.Add($OKButton)


    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(100,75)
    $CancelButton.Size = New-Object System.Drawing.Size(100,20)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({
    $Form.DialogResult = "Cancel"
    $Form.close()
    })
    $Form.Controls.Add($CancelButton)


    $Form.Add_Shown({$Form.Activate()})

    $result = $Form.ShowDialog()

    if($result -eq "OK")
    {
    $SrcServer = $DropDown.SelectedItem.ToString()
    }
    else
    {
    $SrcServer = $null
    }
foreach( $Site in $SiteAttribute.location.Site){
   $var1 = $Site.city    
    If ($var1 -match $SrcServer){
            $SOURCE = $site.server
            $OldSiteGroup = $site.OldSiteGroup
            $OldDFSGroup = $site.OldDFSGroup
            $NewDFSRemote = $site.NewDFSRemote
       } 

}                                                             

$source

# Form building

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 200
$Form.Text = ”User move Destination Location”
$Form.StartPosition = "CenterScreen"


$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
$DropDown.Items.Add($Item) | Out-Null
}

$Form.Controls.Add($DropDown)

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,200)
$DropDownLabel.Text = "Select the user Destination location ?"
$Form.Controls.Add($DropDownLabel)


$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(100,50)
$OKButton.Size = new-object System.Drawing.Size(100,20)
$OKButton.Text = "OK"
$OKButton.Add_Click({
$Form.DialogResult = "OK"
$Form.close()
})
$form.Controls.Add($OKButton)


$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,75)
$CancelButton.Size = New-Object System.Drawing.Size(100,20)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({
$Form.DialogResult = "Cancel"
$Form.close()
})
$Form.Controls.Add($CancelButton)


$Form.Add_Shown({$Form.Activate()})

$result = $Form.ShowDialog()

if($result -eq "OK")
{
$DestServer = $DropDown.SelectedItem.ToString()
}
else
{
$DestServer = $null
}
#The Variable $Sharelocalpath is use in "Part 2 Creating a share on a remote computer" to help WMI
#to create the shared folder remotly using the Localserver Path to create the share if not the good value the U: drive will not map.

foreach( $Site in $SiteAttribute.location.Site){
   $var2 = $Site.city    
    If ($var2 -match $DestServer -and $SrcServer -notmatch $DestServer ){
            $DESTINATION = $site.server
            $Street = $site.street
            $POBox = $site.POBox
            $city = $site.City
            $State = $site.state
            $Zip = $site.zip
            $Country = $site.country
            $OfficePhone = $site.OfficePhone                
            $TargetOU = $site.TargetOU
            $Udrive = $site.Udrive + "$username$"
            $Sharelocalpath = $site.Sharelocalpath + $Username
            $NewSiteGroup = $site.NewSiteGroup
            $NewDFSGroup = $site.NewDFSGroup
            $DESTINATION
            Remove-ADGroupMember -server $DC -Identity $OldSiteGroup -Members $username -Confirm:$false
            Remove-ADGroupMember -server $DC -Identity $OldDFSGroup -Members $username -Confirm:$false
            Add-ADGroupMember -server $DC -Identity $NewDFSRemote -Members $username -Confirm:$false
            Add-ADGroupMember -server $DC -Identity $NewSiteGroup -Members $username -Confirm:$false
            Add-ADGroupMember -server $DC -Identity $NewDFSGroup -Members $username -Confirm:$false            
            }  
 }

我的dropdownarray在两个下拉列表中具有相同的值。但根据我的选择,我的变量会发生变化。谢谢你的帮助。 约翰

1 个答案:

答案 0 :(得分:2)

只是一个肮脏的例子。用这个替换你的表格。您必须再次填写命令,因为我刚刚提取了一个表单来修改它。如果您只是阅读代码,这是非常基本的修改。或者你可以使用你曾经设计过的软件(primalforms)(因为你不理解代码)来制作它。

$DropDownArray = @("Test1", "Test2", "Test3")

# Form building

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 200
$Form.Text = ”User move Destination Location”
$Form.StartPosition = "CenterScreen"

$DropDownFrom = New-Object System.Windows.Forms.ComboBox
$DropDownFrom.Location = New-Object System.Drawing.Size(140,10)
$DropDownFrom.Size = New-Object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
    $DropDownFrom.Items.Add($Item) | Out-Null
}

$Form.Controls.Add($DropDownFrom)

$DropDownFromLabel = New-Object System.Windows.Forms.Label
$DropDownFromLabel.Location = New-Object System.Drawing.Size(10,10)
$DropDownFromLabel.Size = New-Object System.Drawing.Size(100,40)
$DropDownFromLabel.Text = "Select the user Original location ?"
$Form.Controls.Add($DropDownFromLabel)


$DropDownTo = New-Object System.Windows.Forms.ComboBox
$DropDownTo.Location = New-Object System.Drawing.Size(140,50)
$DropDownTo.Size = New-Object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
    $DropDownTo.Items.Add($Item) | Out-Null
}

$Form.Controls.Add($DropDownTo)

$DropDownToLabel = new-object System.Windows.Forms.Label
$DropDownToLabel.Location = new-object System.Drawing.Size(10,50)
$DropDownToLabel.size = new-object System.Drawing.Size(100,40)
$DropDownToLabel.Text = "Select the user Destination location ?"
$Form.Controls.Add($DropDownToLabel)


$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(100,100)
$OKButton.Size = new-object System.Drawing.Size(100,20)
$OKButton.Text = "OK"
$OKButton.Add_Click({
$Form.DialogResult = "OK"
$Form.close()
})
$form.Controls.Add($OKButton)


$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,130)
$CancelButton.Size = New-Object System.Drawing.Size(100,20)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({
$Form.DialogResult = "Cancel"
$Form.close()
})
$Form.Controls.Add($CancelButton)


$Form.Add_Shown({$Form.Activate()})

$result = $Form.ShowDialog()