New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: PowerShell

Querying SQL Servers via PowerShell

298 Views
Copy Code Show/Hide Line Numbers
param (
    [string]$server = ".",
      [string]$instance = $(throw "a database name is required"),
    [string]$query
)
 
$connection = new-object system.data.sqlclient.sqlconnection( `
    "Data Source=$server;Initial Catalog=$instance;Integrated Security=SSPI;");
    
$adapter = new-object system.data.sqlclient.sqldataadapter ($query, $connection)
$set = new-object system.data.dataset
 
$adapter.Fill($set)
 
$table = new-object system.data.datatable
$table = $set.Tables[0]
 
#return table
$table
by David R. Longnecker
  August 31, 2009 @ 12:07pm
Tags:
Comment:
Quick and easy way to query out information without bringing up the darned sql management tools.

by Rick Strahl    August 31, 2009 @ 5:09pm

Wouldn't call that exactly quick and easy :-}

by David R. Longnecker    January 20, 2010 @ 7:37pm

=p Type once, reuse from a module or profile script.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate