Unable to Get Information using PowerShell to Query Oracle

时间:2015-06-30 13:54:03

标签: database oracle powershell

I use PowerShell to query SQL databases, and I am quite familiar with that process. However, I am now tasked with building an automated task that queries Oracle for information.

It seems straight forward: Install proper Oracle DLL's, import them into PS, execute the query much like SQL. However, this is not the case. All I get when I request information is a list called FieldCount. This seems to imply that I am able to see the information, it's just not displaying correctly. I'd like the actual values, and nothing seems to get this for me.

Thanks to anyone who knows anything about this, as my hands are tied and this is the only way I can think of to get this information from Oracle on a scheduled basis. I am not the Oracle admin, I only have read access to this view.

function Get-OracleData($cmdText){
    Add-Type -Path 'C:\app\client\username\product\12.1.0\client_1\odp.net\managed\common\Oracle.ManagedDataAccess.dll'
    $username = 'username'
    $password = 'password'
    $con = New-Object Oracle.ManagedDataAccess.Client.OracleConnection('User Id=$username;Password=$password;Data Source=OracleServerName')
    $con.Open()
    $cmd = New-Object Oracle.ManagedDataAccess.Client.OracleCommand
    $cmd.Connection  = $con
    $cmd.CommandText = $cmdText
    $rdr = $cmd.ExecuteReader()
    if($rdr.Read()){
        return $rdr
    }else{return 0}           
}
Get-OracleData -cmdText '
SELECT em.employee_number,
       em.last_name, 
       em.first_name,
       em.middle_names,
       em.email_address,
       em.start_date, 
       em.term_date,
       em.location_addr_line_1, 
       em.location_city, 
       em.location_work_state,
FROM CustomView em
'

1 个答案:

答案 0 :(得分:1)

在下面的链接中找到答案。通过在$cmd.CommandText = $cmdText位于原始帖子中的行中插入以下代码,我可以得到我需要的内容,并摆脱它下面的内容。

$ds = New-Object system.Data.DataSet
$da = New-Object Oracle.ManagedDataAccess.Client.OracleDataAdapter($cmd)
[void]$da.fill($ds)
return $ds.Tables[0] | Select *

这会返回一个变量,我可以使用$results[0]$results[0].EMPLOYEE_NUMBER等获得第一个条目。

参考:http://poshcode.org/3965 @ line55