使用Powershell访问远程Oracle数据库

时间:2015-06-07 19:49:33

标签: sql oracle powershell database-connection nuget-package

我需要能够连接到我网络上基于Windows 7的Oracle服务器(32位,Oracle XE)。我需要连接的机器运行Windows 7 64位,两台机器都安装了Powershell。

我在64位计算机上安装了Oracle 32位客户端,并在两台计算机上安装了SQL Developer。我想创建一个连接Oracle数据库并运行简单SELECT查询的脚本。我不能让它连接起来。

我尝试过使用ODAC(我想我必须安装Visual Studio才能使用它,因为安装失败)。我听说OleBD可能会容易得多。我想用TNS做到这一点是可能的。谁能在这里给我任何指导?我有一本关于Powershell和Oracle的书,我仍然感到困惑,我无法超越第一阶段。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

以下是我在2015年使用的一个小例子。

# Ora002.ps1
# Need installation of ODAC1120320Xcopy_x64.zip 
# The 32 bit version also exists

# Load the good assembly
Add-Type -Path "C:\oracle\odp.net\bin\4\Oracle.DataAccess.dll"

# Connexion string
$compConStr = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.213.5.123)(PORT=1609)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=COMPEIERE)));User Id=TheLogin;Password=ThePassword;"

# Connexion
$oraConn= New-Object Oracle.DataAccess.Client.OracleConnection($compConStr)
$oraConn.Open()

# Requête SQL
$sql1 = @"
SELECT XX_MYSESSION_ID FROM XX_SILOGIXWSLOG 
  WHERE xx_name='customer_log'
  AND xx_param_4 IS NOT NULL
"@

$command1 = New-Object Oracle.DataAccess.Client.OracleCommand($sql1,$oraConn)

# Execution
$reader1=$command1.ExecuteReader()

$n = 0
while ($reader1.read())
{
  $reader1["XX_MYSESSION_ID"]  
}

# Fermeture de la conexion
$reader1.Close()
$oraConn.Close()

Write-Output $retObj

----- 2017年秋季编辑-----

有一段时间,Oracle编辑了一个完整的.NET托管DLL,可以通过Nugets:

获得
# Download the package if it's not on the disk    
$version = '12.2.1100'
try
{
  if (! $(Test-Path ".\NugetPackages\Oracle.ManagedDataAccess.$version\lib\net40\Oracle.ManagedDataAccess.dll"))
  {
    $ManagedDataAccess = Install-Package Oracle.ManagedDataAccess -Destination ".\NugetPackages" -Force -Source 'https://www.nuget.org/api/v2' -ProviderName NuGet -RequiredVersion $version -ErrorAction SilentlyContinue
  }
  Add-Type -Path ".\NugetPackages\Oracle.ManagedDataAccess.$version\lib\net40\Oracle.ManagedDataAccess.dll"
}
catch [System.Management.Automation.ParameterBindingException]
{
  $global:OracleError = New-Object PSCustomObject -Property @{"StackTrace"=$_.ScriptStackTrace;"Detail" = "Ligne $($_.InvocationInfo.ScriptLineNumber) : $($_.exception.message)";"TimeStamp"=([datetime]::Now)}
  $log = $null
}

# Connexion
$oraConn= New-Object Oracle.ManagedDataAccess.Client.OracleConnection (($compConStr)
$oraConn.Open()

# Requête SQL
$sql1 = @"
SELECT XX_MYSESSION_ID FROM XX_SILOGIXWSLOG 
  WHERE xx_name='customer_log'
  AND xx_param_4 IS NOT NULL
"@

$command1 = New-Object Oracle.ManagedDataAccess.Client.OracleCommand($sql1,$oraConn)

# Execution
$reader1=$command1.ExecuteReader()

$n = 0
while ($reader1.read())
{
  $reader1["XX_MYSESSION_ID"]  
}

# Fermeture de la conexion
$reader1.Close()
$oraConn.Close()

Write-Output $retObj

答案 1 :(得分:2)

我已使用Oracle DLL路径更新了上面的代码。 当我们从Powershell与Oracle连接时,我们连接到托管Oracle服务DLL,可以在下面提到的路径中找到它。

可能是我错了,但下面的代码对我有用。

cls
# Ora002.ps1
# Need installation of ODAC1120320Xcopy_x64.zip 
# The 32 bit version also exists

# Load the good assembly
Add-Type -Path "C:\app\ssz\product\12.1.0\client_1\odp.net\managed\common\Oracle.ManagedDataAccess.dll"

# Production connexion string
$compConStr = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=*<Valid Host>*)(PORT=*<Valid Port>*)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=*<SErviceNameofDB>*)));User Id=*<User Id>*;Password=*<Password>*;"

# Connexion
$oraConn= New-Object Oracle.ManagedDataAccess.Client.OracleConnection($compConStr)
$oraConn.Open()

# Requête SQL
$sql1 = @"SELECT col FROM tbl1 
WHERE col1='test'
"@

$command1 = New-Object Oracle.ManagedDataAccess.Client.OracleCommand($sql1,$oraConn)

# Execution
$reader1=$command1.ExecuteReader()

while ($reader1.read())
{
  $reader1["col"]  
}

# Fermeture de la conexion
$reader1.Close()
$oraConn.Close()

Write-Output $retObj

答案 2 :(得分:1)

接受的答案依赖于客户端安装,并且随着Oracle发布了新的托管版本,它也已过时。您可以使用.NET Oracle library DLL,只需确保在lib文件夹下有所需的DLL文件。

Add-Type -Path "lib\Oracle.ManagedDataAccess.dll"

$query = "select  1 as Col1, 2 as Col2, 3 as Col3 from dual
          union
          select  4 as Col1, 5 as Col2, 6 as Col3 from dual
          union
          select  7 as Col1, 8 as Col2, 9 as Col3 from dual"

$cn   = New-Object Oracle.ManagedDataAccess.Client.OracleConnection -ArgumentList "TNS-ConnectionString-Here"
$cmd  = New-Object Oracle.ManagedDataAccess.Client.OracleCommand    -ArgumentList $query

$cmd.Connection = $cn

try {
    $cn.Open()

    $reader = $cmd.ExecuteReader()

    while ($reader.Read()) {
        $col1 = $reader["Col1"]
        $col2 = $reader["Col2"]
        $col3 = $reader["Col3"]

        Write-Host $col1, $col2, $col3
    }

    $reader.Dispose()

} catch {
    Write-Error $_.Exception.Message
} finally {
    $cmd.Dispose()
    $cn.Dispose()
}