【问题标题】:Powershell: Use choice from one drop down box as variable in anotherPowershell:将一个下拉框中的选择用作另一个下拉框中的变量
【发布时间】:2020-10-30 17:03:10
【问题描述】:

我需要将第一个 DROPDOWN 框的结果存储为一个变量,并用于代替第二个 DROPDOWN 框中的 INSTANCE_NAME。

基本上,一旦做出选择,第二个框的 SELECT 查询需要针对第一个 DROPDOWN 框导致的 SLQ_INSTANCE 中的 MASTER 数据库运行。

我尝试了一些变体,但似乎无法正确使用。有什么建议吗?

提前致谢!

#first DROPDOWN Box
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'SQL Server Name'
$form.Controls.Add($label)
$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(10,40)
$DropDownBox.Size = New-Object System.Drawing.Size(260,20)
$DropDownBox.DropDownHeight = 200
$Form.Controls.Add($DropDownBox)
$wksList= invoke-sqlcmd -query "select * from VIEW_NAME
order by instance_name" -database DATABASE_NAME -serverinstance INSTANCE_NAME
foreach ($wks in $wksList) {
                      $DropDownBox.Items.Add($wks.Instance_Name)
                              } #end foreach
#end first DROPDOWN box
#second DROPDOWN Box
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,90)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Database Name'
$form.Controls.Add($label2)
$DropDownBox2 = New-Object System.Windows.Forms.ComboBox
$DropDownBox2.Location = New-Object System.Drawing.Size(10,110)
$DropDownBox2.Size = New-Object System.Drawing.Size(260,20)
$DropDownBox2.DropDownHeight = 200
$Form.Controls.Add($DropDownBox2)
$wksList2= invoke-sqlcmd -query "select name from sys.databases
where database_id>4
order by name" -database MASTER -serverinstance INSTANCE_NAME
foreach ($wks in $wksList2) {
                     $DropDownBox2.Items.Add($wks.name)
                            } #end foreach
#end second DROPDOWN box

试图在下面发布当前代码....搞砸了编辑:-(,包括乔纳森建议的更改...将不得不开始新帖子.....对不起!!!!!!!!! !!!!

正如我在对 Jonathan 的 cmets 中提到的,这似乎是正确的,但是传递给 -serverinstance 的任何值似乎都不起作用。它“尝试”连接,但 powershell 生成一个错误,指出它无法连接到服务器。

附加说明 - 通过添加 Get-Variable 命令,我确定为 InstanceName 传递的值实际上是空白的。

99.9%...有什么想法吗?再次提前感谢。

通过移动#Second DROPDOWN 框下的$InstanceName = $DropDownBox.SelectedItem 命令,该过程的aprt 现在似乎可以正常工作。我从第一个下拉列表中选择一个服务器,单击按钮,然后将正确的数据库列表填充到第二个下拉列表中。

但是,现在进程底部的 $DBName = $DropDownBox2.text 语句失败,为数据库名称提供空白值..所以,没有备份。

仍在尝试变化,但如果有人看到我错过的任何内容,请告诉我。

我假设我需要添加第二个按钮并单击操作,以将框 2 的结果设置为 db 变量,但我在放置时遇到了问题。

YAY.,...正确输入代码....

这是似乎工作的版本,除了将第二个组合框中的值传递到底部的 $DBName 之外......

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName Microsoft.VisualBasic
 
$form = New-Object System.Windows.Forms.Form
 
$image = [System.Drawing.Image]::Fromfile('Path to image')    
$pictureBox = new-object Windows.Forms.PictureBox  #--instantiates a PictureBox
$pictureBox.width=420
$pictureBox.height=420
$pictureBox.top=20
$pictureBox.left=350
$pictureBox.Image=$image
 
#$form.Font = New-Object System.Drawing.Font("Lucida Console",11,[System.Drawing.FontStyle]::Regular)
 
$form = New-Object System.Windows.Forms.Form
$form.Text = 'MSSQL DATABASE BACKUP UTILITY'
$form.Size = New-Object System.Drawing.Size(800,500)
$form.Font = New-Object System.Drawing.Font("Times New Roman",11,[System.Drawing.FontStyle]::Regular)
$form.StartPosition = 'CenterScreen'
$form.Controls.add($pictureBox)
 
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,400)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
 
#first DROPDOWN Box
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'SQL Instance Name'
$form.Controls.Add($label)

$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(10,40)
$DropDownBox.Size = New-Object System.Drawing.Size(260,20)
$DropDownBox.DropDownHeight = 200
$Form.Controls.Add($DropDownBox)

$wksList= invoke-sqlcmd -query "select * from VIEW NAME
    order by instance_name" -database DBA -serverinstance INSTANCE_NAME

 
foreach ($wks in $wksList)
    { $DropDownBox.Items.Add($wks.Instance_Name) }
 
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,80)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Set Instance"

$Form.Controls.Add($Button)

#Add Button event
 $Button.Add_Click(
{   
    #second DROPDOWN Box
    $InstanceName = $DropDownBox.SelectedItem
    $label2 = New-Object System.Windows.Forms.Label
    $label2.Location = New-Object System.Drawing.Point(10,120)
   $label2.Size = New-Object System.Drawing.Size(280,20)
    $label2.Text = 'Database Name'
    $form.Controls.Add($label2)

    $DropDownBox2 = New-Object System.Windows.Forms.ComboBox
    $DropDownBox2.Location = New-Object System.Drawing.Size(10,140)
    $DropDownBox2.Size = New-Object System.Drawing.Size(260,20)
    $DropDownBox2.DropDownHeight = 200
    $Form.Controls.Add($DropDownBox2)

    $wksList2= invoke-sqlcmd -query "select name from sys.databases
    where database_id>4
    order by name" -database MASTER -serverinstance $InstanceName

    foreach ($wks in $wksList2)
        { $DropDownBox2.Items.Add($wks.name) }

})

#end second DROPDOWN box
 
# third text box
$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(10,220)
$label3.Size = New-Object System.Drawing.Size(280,20)
$label3.Text = 'Desired Backup Location'
$form.Controls.Add($label3)
 
$textBox3 = New-Object System.Windows.Forms.TextBox
$textBox3.Location = New-Object System.Drawing.Point(10,240)
$textBox3.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox3)
#end third text box
 
# fourth text box
$label4 = New-Object System.Windows.Forms.Label
$label4.Location = New-Object System.Drawing.Point(10,280)
$label4.Size = New-Object System.Drawing.Size(280,20)
$label4.Text = 'Desired Backup Name'
$form.Controls.Add($label4)
 
$textBox4 = New-Object System.Windows.Forms.TextBox
$textBox4.Location = New-Object System.Drawing.Point(10,300)
$textBox4.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox4)
#end fourth text box
 
# fifth text box
$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(10,340)
$label5.Size = New-Object System.Drawing.Size(280,20)
$label5.Text = 'Your Email Address'
$form.Controls.Add($label5)
 
$textBox5 = New-Object System.Windows.Forms.TextBox
$textBox5.Location = New-Object System.Drawing.Point(10,360)
$textBox5.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox5)
#end fifth text box
 
$form.Topmost = $true
 
$form.Add_Shown({$DropDownBox.Select()})
$result = $form.ShowDialog()
 
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$Server = $DropDownBox.text
$DBName = $DropDownBox2.text
$SharedFolder = $textBox3.text
$BUName = $textBox4.text
$mail = $textBox5.text
 
Get-Variable
 
   <#$Date = Get-Date -format yyyyMMdd#>
Backup-SqlDatabase  -ServerInstance $server `
                    -Database $DBName `
                    -CopyOnly `
                    -CompressionOption on `
                    -BackupFile "$($SharedFolder)\$DBName-$BUName.bak" `
                    -BackupAction Database `
                    -checksum
 
#SMPT Section
 
$From = "SUPPORT EMAIL"
$To = $mail
$Subject = "RECENT MSSQL BACKUP REQUEST"
$Body = "Your MSSQL database backup from $server, $DBName, has been backed up to $SharedFolder. Please check the directory path you specified for your backup. If you have any problems, fwd this email to SUPPORT EMAIL for assistance."
$PSEmailServer = "SMTP SERVER"
 
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $PSEmailserver

【问题讨论】:

    标签: sql powershell variables dropdown


    【解决方案1】:

    此示例使用一个按钮来控制工作流程。在这种情况下,用户将在第一个下拉框中选择实例。当他们单击按钮时,所选名称将存储在$InstanceName 中,可用于第二个下拉列表的查询

    $form = New-Object System.Windows.Forms.Form
    
    #first DROPDOWN Box
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'SQL Server Name'
    $form.Controls.Add($label)
    
    $DropDownBox = New-Object System.Windows.Forms.ComboBox
    $DropDownBox.Location = New-Object System.Drawing.Size(10,40)
    $DropDownBox.Size = New-Object System.Drawing.Size(260,20)
    $DropDownBox.DropDownHeight = 200
    $Form.Controls.Add($DropDownBox)
    
    $wksList= invoke-sqlcmd -query "select * from VIEW_NAME
        order by instance_name" -database DATABASE_NAME -serverinstance INSTANCE_NAME
    
    
    foreach ($wks in $wksList) 
        { $DropDownBox.Items.Add($wks.Instance_Name) } 
    
    
    $InstanceName = $DropDownBox.SelectedItem
    
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(10,60)
    $Button.Size = New-Object System.Drawing.Size(120,23)
    $Button.Text = "Select DB"
    
    $Form.Controls.Add($Button)
    
    #Add Button event 
    $Button.Add_Click(
    {    
        #second DROPDOWN Box
        $label2 = New-Object System.Windows.Forms.Label
        $label2.Location = New-Object System.Drawing.Point(10,90)
        $label2.Size = New-Object System.Drawing.Size(280,20)
        $label2.Text = 'Database Name'
        $form.Controls.Add($label2)
    
        $DropDownBox2 = New-Object System.Windows.Forms.ComboBox
        $DropDownBox2.Location = New-Object System.Drawing.Size(10,110)
        $DropDownBox2.Size = New-Object System.Drawing.Size(260,20)
        $DropDownBox2.DropDownHeight = 200
        $Form.Controls.Add($DropDownBox2)
    
        $wksList2= invoke-sqlcmd -query "select name from sys.databases
            where database_id>4
            order by name" -database MASTER -serverinstance $InstanceName
    
        foreach ($wks in $wksList2) 
            { $DropDownBox2.Items.Add($wks) }
    
    })
    
    $form.ShowDialog()
    

    【讨论】:

    • 这让我非常接近,谢谢!唯一的问题似乎是将参数传递给第二个 SQLCMD 命令。它尝试连接到服务器以列​​出数据库,但出错了。我假设它找不到随代码更改传递的 -serverinstance 的任何值。
    【解决方案2】:

    $InstanceName = $DropDownBox.SelectedItem; 替换为$DropDownBox.Add_SelectedIndexChanged({ $InstanceName = $DropDownBox.SelectedItem; }),你就是gtg。

    【讨论】:

    • 谢谢,但这会在第二个框中提供一个空白下拉列表,而不是数据库列表。
    • $DropDownBox.Add_SelectedIndexChanged({.....}) 之后添加$DropDownBox.SelectedIndex = 0。这将选择组合框中的第一个元素并更新$InstanceName。或者您可以通过$InstanceName = $DropDownBox.SelectedItem手动设置$InstanceName
    猜你喜欢
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多