Monthly Archives: December 2008

Powershell Script to Connect to SQL Server and get some information

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}

Read More

PowerShell Script to compare two lists

A quick script to compare two arrays.

$arrFirst = get-content First.txt
$arrSecond = get-content Second.txt

New-Item “C:MyworkplacepsFirstInSecond.txt” -Type file
New-Item “C:MyworkplacepsFirstNotInSecond” -Type file

Foreach ($First in $arrFirst)
{
If ($arrSecond -contains $First)
{Add-content “C:MyworkplacepsFirstInsecond.txt” $First}
Else
{Add-content “C:MyworkplacepsFirstNotInSecond.txt” $First}

Read More