我正在为我的一个脚本使用下拉菜单,我调用它两次以在两个不同的窗口上获得两个不同的值。我想优化我的脚本并在同一个窗口中创建一个带有两个下拉框的窗口。
看我的脚本:
$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
}
}
我的下拉数组在两个下拉列表中具有相同的值。但是根据我的选择,我的 var 将会改变。谢谢你的帮助。约翰