- function RunQueryOnDB ($DBServer, $DBCatalog, $DBQuery, $DBUid, $DBUidPWD)
- {
- $SqlServer = $DBServer
- $SqlCatalog = $DBCatalog
- $SqlQuery = $DBQuery
- $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
- $SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlCatalog; Uid = $DBUid; Pwd = $DBUidPWD"
- $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
- $SqlCmd.CommandText = $SqlQuery
- $SqlCmd.Connection = $SqlConnection
- $SQlCmd.CommandTimeout = 120
- $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
- $SqlAdapter.SelectCommand = $SqlCmd
- $DSet = New-Object System.Data.DataSet
- $SqlAdapter.Fill($DSet) | out-null
- $SqlConnection.Close()
- $DSet.Tables[0]
- }
How to use it :
- # --------- set the DB Server and Catalog
- $DBServerName = "MyDBServer"
- $DBName = "MyDBNAme"
- # --------- Set the Query
- $Query = "Select PrincipalName from MyDB"
- # --------- Set the credentials to use to query the DB
- $Account = "MyAccount"
- $pwd = "MyPassword"
- # --------- Variable initialization
- $SCOMGrpName = "MyGroupName"
- # --------- Query The DB
- $Result = RunQueryOnDB $DBServerName $DBName $Query $Account $pwd
The script wil return a table $Result that can also be re-used.
This posting is provided "AS IS" with no warranties.
No comments:
Post a Comment