AnoopGeorge.com

Powershell Script to Connect to SQL Server and get some information

22.12.2008

Blog

A quick script to connect to an sql sserver and get the information from a table.  In this case find the SQL Proxy accounts from the msdb database.

 # SQL Server and Database Information
 $SqlServer = “.”;
 $SqlCatalog = “msdb”;

 # SQL Query Statement
 $q= “EXEC dbo.sp_help_proxy”

 $cn = new-object system.data.SqlClient.SqlConnection(“Data Source=$SqlServer;Integrated Security=SSPI;Initial Catalog=$SqlCatalog”);
 $ds = new-object “System.Data.DataSet” “dbo”

 $da = new-object “System.Data.SqlClient.SqlDataAdapter” ($q, $cn)

 $junk = $da.Fill($ds)
 
 $proxyAccounts = new-object “System.Data.DataTable” “dtPersonData”
 $proxyAccounts = $ds.Tables[0]

 $proxyAccounts | FOREACH-OBJECT {“” + $_.Name}

TAGS:

You must be logged in to post a comment.