Richard Gardner
asked on
Powershell GUI pull downs populated by MS SQL table
I am generating a Powershell GUI with pull down menus. I am trying to populate these pull down menu's from a table in a SQL database. Within the pull down I want to be the value to be set by a table called 'AD_USERNAME' and displayname to be set by a table called 'DISPLAY_NAME'. I know how to display the contents on the screen but I'm not sure how to populate the pull downs?
I know I need to use Foreach to loop through the table but I can't work out how. I included the $name variables which I can use to print the table information on screen.
Here is the code I have so far -
$SQLServer = 'SQL1'
$SQLDBName = 'test'
$SqlQuery = 'select * from dbo.SMS_CONTACT'
$SqlConnection = New-Object System.Data.SqlClient.SqlC onnection
$SqlConnection.ConnectionS tring = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlC ommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlD ataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) | Out-Null
$SqlConnection.Close()
[void] [System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.Drawing" )
[void] [System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.Windows. Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,40 0)
$DropDownBox = New-Object System.Windows.Forms.Combo Box
$DropDownBox.Location = New-Object System.Drawing.Size(20,50)
$DropDownBox.Size = New-Object System.Drawing.Size(180,20 )
$DropDownBox.DropDownHeigh t = 200
$DropDownBox.DropDownStyle = 2
$DropDownBox.Items.Add("$t able")
$Form.Controls.Add($DropDo wnBox)
foreach ($Row in $DataSet.tables)
{
$DropDownBox.Items.Add($Ro w)
#$name = $Row.Item('AD_USERNAME'), $Row.Item('DISPLAY_NAME')
#$name
}
$Form.Add_Shown({$Form.Act ivate()})
[void] $Form.ShowDialog()
I know I need to use Foreach to loop through the table but I can't work out how. I included the $name variables which I can use to print the table information on screen.
Here is the code I have so far -
$SQLServer = 'SQL1'
$SQLDBName = 'test'
$SqlQuery = 'select * from dbo.SMS_CONTACT'
$SqlConnection = New-Object System.Data.SqlClient.SqlC
$SqlConnection.ConnectionS
$SqlCmd = New-Object System.Data.SqlClient.SqlC
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlD
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
[void] [System.Reflection.Assembl
[void] [System.Reflection.Assembl
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,40
$DropDownBox = New-Object System.Windows.Forms.Combo
$DropDownBox.Location = New-Object System.Drawing.Size(20,50)
$DropDownBox.Size = New-Object System.Drawing.Size(180,20
$DropDownBox.DropDownHeigh
$DropDownBox.DropDownStyle
$DropDownBox.Items.Add("$t
$Form.Controls.Add($DropDo
foreach ($Row in $DataSet.tables)
{
$DropDownBox.Items.Add($Ro
#$name = $Row.Item('AD_USERNAME'), $Row.Item('DISPLAY_NAME')
#$name
}
$Form.Add_Shown({$Form.Act
[void] $Form.ShowDialog()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER