My helper function for executing a DMV query:
Function DMV_Query($DMV_Query) {
## Prepare the connection string based on information provided
$connectionString = "Provider=msolap;Data Source=$AS_Server;Initial Catalog=$CUBE;"
## Connect to the data source and open
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand
$command.Connection = $connection
$command.CommandText = $DMV_Query
$connection.Open()
## Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter #$command
$adapter.SelectCommand = $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
## Return all of the rows from their query
$dataSet.Tables[0]
}
Sample call:
$dmvResult = DMV_Query 'SELECT [SESSION_ID], [SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS'
$dmvResult
Sample of the content stored in $dmvResult
:
PS> $dmvResult | ConvertTo-Csv
"SESSION_ID","SESSION_SPID"
"D0kl8975r6df65","5567"
"fghu5r67f65","2234"
I want to select all columns from the $dmvResult
variable and insert them into a SQL table. Is this the way I can select from a variable?
# Doesn't work.
INSERT INTO [dbo].[Table]
SELECT * FROM @dmvResult