在 Powershell 中使用提供程序 MSDAORA 的 ADODB

ADODB with provider MSDAORA in Powershell

我想使用 powershell 从 Oracle 数据库中读取数据以执行一些评估并将包含结果的电子邮件发送给收件人列表。

首先,我开始对 Excel-VBA 中的数据库读数进行编程,以便对结果有更好的视觉体验,效果很好。

Sub ADODBOracle()
    Dim ADOConnection As Object : Set ADOConnection = CreateObject("ADODB.Connection")
    Dim ADORecordSet As Object : Set ADORecordSet = CreateObject("ADODB.Recordset")

    'Early binding and ADODB related enums require a reference to Microsoft ActiveX Data Objects 6.1 Library
    'Dim ADOConnection As New ADODB.Connection
    'Dim ADORecordSet As New ADODB.Recordset

    Dim Field As Variant

    Dim i As Long
    Dim j As Long

    ADOConnection.ConnectionString = "Provider=MSDAORA;Data Source=XSource;User Id=user;Password=pass;"
    ADOConnection.Open

    With ADORecordSet
        .ActiveConnection = ADOConnection
        .CursorLocation = 3 'adUseClient
        .CursorType = 3 'adOpenStatic
        .LockType = 1 'adLockReadOnly
        .Open "SELECT * from MY.Database WHERE TIMESTAMP LIKE '%11.03.20%'"
    End With

    ThisWorkbook.Sheets(1).Cells.Clear

    j = 1
    For Each Field In ADORecordSet.Fields
        ThisWorkbook.Sheets(1).Cells(1, j) = Field.Name
        ThisWorkbook.Sheets(1).Columns(j).EntireColumn.NumberFormat = "@"
        j = j + 1
    Next

    i = 2
    ADORecordSet.MoveFirst
    Application.ScreenUpdating = False

    While Not ADORecordSet.EOF: DoEvents
        For j = 1 To ADORecordSet.Fields.Count
            ThisWorkbook.Sheets(1).Cells(i, j) = ADORecordSet.Fields(ThisWorkbook.Sheets(1).Cells(1, j).Text).Value
        Next j
        i = i + 1
        ADORecordSet.MoveNext
    Wend
    Application.ScreenUpdating = True
    ADOConnection.Close
End Sub

当将这段确切的代码重写到 Powershell 中时,我奇怪地收到一条错误消息,提示我未安装提供程序?

#CursorLocationEnum
$adUseServer = 2
$adUseClient = 3

#LockTypeEnum
$adLockReadOnly = 1
$adLockPessimistic = 2
$adLockOptimistic = 3
$adLockBatchOptimistic = 4

#CursorTypeEnum
$adOpenForwardOnly = 0
$adOpenKeyset = 1
$adOpenDynamic = 2
$adOpenStatic = 3

$ADOCon = New-Object -ComObject ADODB.Connection
$ADORec = New-Object -ComObject ADODB.Recordset

$ADOCon.ConnectionString = "Provider=MSDAORA;Data Source=XSource;User Id=user;Password=pass;"
$ADOCon.Open()
#ERROR! Provider not found / not installed

$ADORec.ActiveConnection = $ADOCon
$ADORec.CursorLocation = $adUseClient
$ADORec.CursorType = $adOpenStatic
$ADORec.LockType = $adLockReadOnly

$ADORec.Open("SELECT * from MY.Database WHERE TIMESTAMP LIKE '%11.03.20%'")
#to be done
$ADOCon.Close()

另一个问题:如何在 Powershell 中使用 ADODB 枚举的值(例如 CursorLocationEnum)而不为每个值创建一个变量?

我找到了问题的答案。 MSDAORA 驱动程序仅在 Powershell 会话在 x86 模式下为 运行 时可用,但在 AMD64 模式下不可用...