Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Powershell: SQLCMD, substituting variable for -serverinstance parameter

Powershell: SQLCMD, substituting variable for -serverinstance parameter

Powershell: SQLCMD, substituting variable for -serverinstance parameter


I need to get the results of the first DROPDOWN box stored as a variable, and used in place of INSTANCE_NAME in the second DROPDOWN box.

Essentially, the SELECT query from the second box needs to be run against the MASTER database in the SLQ_INSTANCE that the first DROPDOWN box results in once a choice has been made.

I've tried a few variations and can't seem to get it right. Any suggestions?

Thanks in advance!

CODE -->

#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'
$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
$wksList= invoke-sqlcmd -query "select * from VIEW_NAME
order by instance_name" -database DATABASE_NAME -serverinstance INSTANCE_NAME
foreach ($wks in $wksList) {
                              } #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'
$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
$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) {
                            } #end foreach
#end second DROPDOWN box 

RE: Powershell: SQLCMD, substituting variable for -serverinstance parameter

Hi you seem to be using WPF forms so this may work..

CODE --> PowerShell

function BuildDropDown1 { 
    $YearFolders = Get-ChildItem -path $GalleryFolder.Text -Filter 2* 
    foreach ($SubFolder in $YearFolders)
        $WhatYear = "{0}" -f $SubFolder.Name

function BuildDropDown2 {
     $Gallery = -join($GalleryFolder.Text,$combobox0.text,"\")
     $GalleryFolders = get-childitem -path  $Gallery -Directory
    foreach ($GalFolder in $GalleryFolders)
        $WhatFolder = "{0}" -f $GalFolder.Name

triggered by

CODE --> Powershell

$GalleryFolder.Add_TextChanged({ BuildDropDown1 })
$GallerySubFolder.Add_SelectedIndexChanged({  BuildDropDown2  }) 

This isnt using SQL but can probably be adapted but runs a dir against a folder, then populates the 2nd box with the filtered names (2*) of the subfolders into the second dropdown, then results of the subfolders inside them in a 3rd drop down..?

This example works on am image gallery folder structure similat to

Gallery 1
Gallery 2
Gallery 1
Gallery 2


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close