Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 甲骨文及;Powershell在批处理文件中的不同行为_Windows_Oracle_Powershell_Batch File - Fatal编程技术网

Windows 甲骨文及;Powershell在批处理文件中的不同行为

Windows 甲骨文及;Powershell在批处理文件中的不同行为,windows,oracle,powershell,batch-file,Windows,Oracle,Powershell,Batch File,我已经构建了一个连接到Oracle数据库并进行各种导出的脚本 当我从Windows的“开始”菜单启动powershell.exe并按如下方式运行脚本时,它可以正常工作: .\OracleConnect set path=%~dp0 set file=OracleConnect.ps1 C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass %file% 但是,当

我已经构建了一个连接到Oracle数据库并进行各种导出的脚本

当我从Windows的“开始”菜单启动powershell.exe并按如下方式运行脚本时,它可以正常工作:

.\OracleConnect
    set path=%~dp0
    set file=OracleConnect.ps1
    C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass %file%
但是,当我试图通过从批启动来自动化完全相同的脚本时,会收到一条错误消息:

“Oracle.DataAccess.Client.OracleConnection的类型初始值设定项引发异常”

正如我所说,这两种情况下的脚本是相同的

我的.bat文件如下所示:

.\OracleConnect
    set path=%~dp0
    set file=OracleConnect.ps1
    C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass %file%
我还尝试了其他powershell.exe路径:

    C:\Windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass %file%
没有任何成功

以下是我的完整脚本:

$scriptpath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Set-Location $scriptpath
[void][Reflection.Assembly]::LoadFile($scriptpath + "\Oracle.DataAccess.dll")
$logpath = $scriptpath + "\Export.txt" 

$connectionpath = read-host 'connection file [optional]'
if($connectionpath)
{
    $connectionstring = get-content $connectionpath
}
else
{
    $server = read-host 'host name'
    $user = read-host 'user name'
    $pass = read-host 'password'
    $database = read-host 'service'
    $connectionstring = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=$server)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=$database)));User Id=$user;Password=$pass;"
}

$echoon = read-host 'echo on (y - n) [default off]'

function global:get-result($query)
{
    $oracleconnection = new-object Oracle.DataAccess.Client.OracleConnection
    $oracleconnection.connectionstring = $connectionstring
    $oracleconnection.Open()
    $oraclecommand = $oracleconnection.CreateCommand()
    $oraclecommand.CommandText = $query
    $reader = $oraclecommand.ExecuteReader()

    $writer = new-object System.IO.StreamWriter($logpath)

    $counter = 0
    write-output ""
    while ($reader.Read()) 
    {
        $columns = ""
        $columnslog = ""
        $tab = "" 
        $tablog = "" 
        if($counter -eq 0)
        {
            for ($i= 0; $i -lt $reader.FieldCount; $i++) 
            {
                $columns = $columns + " | " + $reader.GetName($i)
                $columnslog = $columnslog + "`t" + $reader.GetName($i).ToUpper()
            }
            $writer.WriteLine($columnslog)

            if($echoon -eq "y")
            {
                write-output $columns
                write-output ""
            }
        }

        for ($i= 0; $i -lt $reader.FieldCount; $i++) 
        {
            $tab = $tab + " | " + $reader.GetValue($i).ToString()
            $tablog = $tablog + "`t" + $reader.GetValue($i).ToString()
        }

        $writer.WriteLine($tablog)

        if($echoon -eq "y")
        {
            write-output $tab 
            write-output ""
        }

        $counter = $counter + 1
    }

    $oracleconnection.Close()
    $writer.Close()
}

function global:update-query($query)
{
    $oracleconnection = new-object Oracle.DataAccess.Client.OracleConnection
    $oracleconnection.connectionstring = $connectionstring
    $oracleconnection.Open()
    $oraclecommand = $oracleconnection.CreateCommand()
    $oraclecommand.CommandText = $query
    $reader = $oraclecommand.ExecuteNonQuery()
}

function global:readscript($path)
{
    $inenc = [System.Text.Encoding]::UTF7
    $reader = new-object System.IO.StreamReader($path, $inenc)
    $finalquery = ""

    while ($line = $reader.ReadLine())
    {
        $finalquery += $line
    }
    $reader.close()

    return $finalquery
}

write-output "Enter query or type 'exit' to quit..."
while($true)
{
    $value = read-host " "
    if($value -eq "exit")
    {
        $save = read-host "Save connection ? [Y:yes N:no]"
        if($save.ToLower() -eq "y")
        {
            $path = read-host "Connection path"
            if(test-path $path)
            {
                rm $path
            }
            new-item -type file -force $path
            $writer = New-Object System.IO.StreamWriter $path
            $writer.WriteLine($connectionstring)
            $writer.Close()
        }
        else
        {

        }
        return
    }
    else 
    {
        if($value.Tolower().EndsWith(".sql") -eq $false)
        {
            if ($value.ToLower().StartsWith("select") -or $value.ToLower().StartsWith("show"))
            {
                get-result($value)
            }
            else
            {
                update-query($value)
            }
        }
        else
        {
            $scriptquery = readscript $value
            if ($scriptquery.ToLower().StartsWith("select") -or $scriptquery.ToLower().StartsWith("show"))
            {
                get-result($scriptquery)
            }
            else
            {
                update-query($scriptquery)
            }
        }
    }
}
编辑:

我现在尝试捕获异常,并在此处添加了try-catch指令:

try 
{
    $oracleconnection = new-object Oracle.DataAccess.Client.OracleConnection
}
catch
{
     write-host $_.Exception.ToString()
}
我得到的信息是: Oracle.DataAccess.Client.OracleException:提供程序与Oracle客户端版本不兼容


我仍然不明白为什么Oracle版本会出现问题,因为当我从powershell窗口运行它时,它工作正常。

显示您的powershell脚本。使用脚本更新。
%Oracle\u HOME%
在这两种情况下是否相同?我如何验证?我添加了echo%ORACLE\u HOME%,但它似乎不是正确的命令路径中是否有ORACLE库“oci.dll”?PS中的输出路径:$env:PATH