Created
June 21, 2012 13:41
-
-
Save codebrane/2965778 to your computer and use it in GitHub Desktop.
Powershell to extract user information from MSSQL Blackboard database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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