Skip to content

Instantly share code, notes, and snippets.

@codebrane
Created June 21, 2012 13:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebrane/2965778 to your computer and use it in GitHub Desktop.
Save codebrane/2965778 to your computer and use it in GitHub Desktop.
Powershell to extract user information from MSSQL Blackboard database
#
# MSSQL connection section from:
# http://www.systemcentercentral.com/BlogDetails/tabid/143/indexid/60012/Default.aspx
#
$SQLServer = 'HOSTNAME'
$SQLDBName = 'DATABASE_NAME'
$SqlQuery = 'select * from users'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
clear
write-host 'There are ' $DataSet.tables[0].rows.count ' users:'
foreach ($Row in $DataSet.Tables[0].Rows)
{
$name = $Row.Item('firstname'), $Row.Item('lastname') -join " "
$last_login_date = [string]$Row.Item('last_login_date')
if (!$last_login_date)
{
$last_login_date = 'NEVER LOGGED IN'
}
$user = $Row.Item('user_id'), $name, $Row.Item('email'), $last_login_date -join ","
$users = $users + $user + "`r`n"
write-host $user
}
out-file -filepath c:\users.txt -inputobject $users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment