使用Powershell访问远程Oracle数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Powershell访问远程Oracle数据库相关的知识,希望对你有一定的参考价值。
我需要能够连接到我的网络上基于Windows 7的Oracle服务器(32位,Oracle XE)。我需要连接的机器运行Windows 7 64位,两台机器上都安装了Powershell。
我在64位计算机上安装了Oracle 32位客户端,并在两台计算机上安装了SQL Developer。我想创建一个连接Oracle数据库并运行简单SELECT查询的脚本。我无法连接它。
我尝试过使用ODAC(我认为我必须安装Visual Studio才能使用它,因为安装失败)。我听说OleBD可能会容易得多。我想用TNS做到这一点是可能的。谁能在这里给我任何指导?我有一本关于Powershell和Oracle的书,我仍然感到困惑,我无法超越第一阶段。
任何帮助将不胜感激。
这是我在2015年使用的一个小例子。
# Ora002.ps1
# Need installation of ODAC1120320Xcopy_x64.zip
# The 32 bit version also exists
# Load the good assembly
Add-Type -Path "C:oracleodp.netin4Oracle.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 ".NugetPackagesOracle.ManagedDataAccess.$versionlib
et40Oracle.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 ".NugetPackagesOracle.ManagedDataAccess.$versionlib
et40Oracle.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
我已使用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:appsszproduct12.1.0client_1odp.netmanagedcommonOracle.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
接受的答案依赖于客户端安装,并且随着Oracle发布新的托管版本,它也已过时。您可以使用.NET Oracle library DLL,只需确保您在lib文件夹下有所需的DLL文件。
Add-Type -Path "libOracle.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()
}
以上是关于使用Powershell访问远程Oracle数据库的主要内容,如果未能解决你的问题,请参考以下文章
尝试使用模拟在远程计算机上运行 PowerShell 脚本时“不允许请求的注册表访问”
需要通过 powershell 使用 invoke-command 远程访问第二个跃点
Powershell - 运行位于远程机器上的脚本,该脚本反过来从网络共享访问文件