Skip to content

Instantly share code, notes, and snippets.

@jbutters
Last active August 29, 2015 14:15
Show Gist options
  • Save jbutters/07e867f3cb624391a75e to your computer and use it in GitHub Desktop.
Save jbutters/07e867f3cb624391a75e to your computer and use it in GitHub Desktop.
PowerShell query students then export as csv for Naviance
<# credit to http://blogs.technet.com/b/heyscriptingguy/archive/2012/12/04/use-oracle-odp-net-and-powershell-to-simplify-data-access.aspx#>
#Just modify the Connection String here
$conString="User Id=<your db query username>;Password=<your password>;Data Source=<your powershool db IP>/<db sid>"
#Just modify the SQL here:
$sqlString=@"
/*
Product: student auto import
Data Type: SIS
Entity: STUDENT
Author:
Modified:
*/
SELECT
students.student_number as Student_ID,
students.SCHED_YEAROFGRADUATION as Class_Year,
students.Last_Name as Last_Name,
students.SchoolID as Campus_ID,
students.First_Name as First_Name,
students.Gender as Gender,
students.Ethnicity as Ethnicity,
TO_CHAR(students.DOB, 'MM/DD/YYYY') as DOB,
('FC' || students.student_number) as FC_User_Name, /* some method to create unique usernames for each student */
'<some default password>' as FC_Password
FROM
PS.STUDENTS
WHERE
ps.STUDENTS.grade_level > '05' and ps.students.enroll_status = '0' and ps.students.SCHOOLID != '99'
"@
#location of your ODP.NET
Add-Type -Path "F:\autoExport\odp.net\managed\common\Oracle.ManagedDataAccess.dll"
function Get-OracleResultDa
{
param (
[Parameter(Mandatory=$true)]
[ValidateScript({$_ -match '\bdata source\b'})]
[string]$conString,
[ValidateScript({$_ -match '\bselect\b'})]
[Parameter(Mandatory=$true)]
[string]$sqlString
)
$resultSet=@()
try {
$con = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($conString)
$cmd=$con.CreateCommand()
$cmd.CommandText= $sqlString
$da=New-Object Oracle.ManagedDataAccess.Client.OracleDataAdapter($cmd);
$resultSet=New-Object System.Data.DataTable
[void]$da.fill($resultSet)
} catch {
Write-Error ($_.Exception.ToString())
} finally {
if ($con.State -eq 'Open') { $con.close() }
}
$resultSet
}
#this is the line where specify the location for the export and you have to specify the objects you want after the 1st pipe
Get-OracleResultDa $conString $sqlString | Select-Object Student_ID,Class_Year,Last_Name,Campus_ID,First_Name,Gender,Ethnicity,DOB,FC_User_Name,FC_Password | Export-Csv -NoTypeInformation F:\autoExport\exports\naviance_students.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment