Powershell ArgumentOutofRange异常,我不知道为什么

Powershell ArgumentOutofRange异常,我不知道为什么,powershell,ado,ms-access-2013,Powershell,Ado,Ms Access 2013,我正在构建一个PowerShell脚本,用于检索存储在MS Access 2013数据库文件中的一些信息。查询是根据机器上收集的其他数据动态生成的。我采用了完全模块化的方法,将所有的数据库功能和管理放在一个单独的模块中,我用Using module语句引入了该模块。该功能相当大,因此我认为这样可以更容易地管理代码。除了openRecordSet函数外,所有功能都运行良好 以下是我的数据库类以及connectToDatabase方法和openRecordSet方法的构造函数: 这是给我带来问题的代

我正在构建一个PowerShell脚本,用于检索存储在MS Access 2013数据库文件中的一些信息。查询是根据机器上收集的其他数据动态生成的。我采用了完全模块化的方法,将所有的数据库功能和管理放在一个单独的模块中,我用Using module语句引入了该模块。该功能相当大,因此我认为这样可以更容易地管理代码。除了openRecordSet函数外,所有功能都运行良好

以下是我的数据库类以及connectToDatabase方法和openRecordSet方法的构造函数:

这是给我带来问题的代码。它位于另一个类中,在执行到达此行时已成功创建数据库对象和连接:

$RecSet = $this.database.openRecordSet($query, 3, 1)
使用VisualStudio代码的调试模式,一个Write Host语句并在MS Access中运行它,我已经确认$query中的SQL语句是正确的。第二个参数是游标类型,在本实例中是动态的,第三个参数是锁类型,在本实例中是只读的。我使用了在数据库类中定义的常量,但将它们更改为原始数值,以确认这些并没有导致问题。错误没有改变。以下是错误消息:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index At C:\Users\FakeUser\Documents\MySuperScript\modules\userprofiles.psm1:20 char:21 + ... $RecSet = $this.database.openRecordSet($query, 3, 1) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], ArgumentOutOfRangeException + FullyQualifiedErrorId : System.ArgumentOutOfRangeException 我不知道是什么导致了这个错误$查询是一个字符串,$this.connection是一个comObject连接。我在不同的脚本中使用了相同的语法,没有任何问题,只是在我开始使用模块时才发生了这种情况。当然,这是一个简单的语法问题,或者是我忽略了的其他简单问题

打开方法的返回类型为void。该错误会弹出,因为您正在向对象强制转换void以返回该对象。 下面是ADODB.Recordset的列表

_xClone():ADODB.Recordset (Method)
_xResync(AffectRecords:ADODB.AffectEnum):System.Void (Method)
_xSave(FileName:System.String, PersistFormat:ADODB.PersistFormatEnum):System.Void (Method)
AbsolutePage:ADODB.PositionEnum (Get Property)
AbsolutePage:System.Void (Set Property)
AbsolutePosition:ADODB.PositionEnum (Get Property)
AbsolutePosition:System.Void (Set Property)
ActiveCommand:System.Object (Get Property)
ActiveConnection:System.Object (Get Property)
ActiveConnection:System.Void (Set Property)
AddNew(FieldList:System.Object, Values:System.Object):System.Void (Method)
BOF:System.Boolean (Get Property)
Bookmark:System.Object (Get Property)
Bookmark:System.Void (Set Property)
CacheSize:System.Int32 (Get Property)
CacheSize:System.Void (Set Property)
Cancel():System.Void (Method)
CancelBatch(AffectRecords:ADODB.AffectEnum):System.Void (Method)
CancelUpdate():System.Void (Method)
Clone(LockType:ADODB.LockTypeEnum):ADODB.Recordset (Method)
Close():System.Void (Method)
Collect:System.Object (Get Property)
Collect:System.Void (Set Property)
CompareBookmarks(Bookmark1:System.Object, Bookmark2:System.Object):ADODB.CompareEnum (Method)
CursorLocation:ADODB.CursorLocationEnum (Get Property)
CursorLocation:System.Void (Set Property)
CursorType:ADODB.CursorTypeEnum (Get Property)
CursorType:System.Void (Set Property)
DataMember:System.String (Get Property)
DataMember:System.Void (Set Property)
DataSource:System.Object (Get Property)
DataSource:System.Void (Set Property)
Delete(AffectRecords:ADODB.AffectEnum):System.Void (Method)
EditMode:ADODB.EditModeEnum (Get Property)
EOF:System.Boolean (Get Property)
Fields:ADODB.Fields (Get Property)
Filter:System.Object (Get Property)
Filter:System.Void (Set Property)
Find(Criteria:System.String, SkipRecords:System.Int32, SearchDirection:ADODB.SearchDirectionEnum, Start:System.Object):System.Void (Method)
GetRows(Rows:System.Int32, Start:System.Object, Fields:System.Object):System.Object (Method)
GetString(StringFormat:ADODB.StringFormatEnum, NumRows:System.Int32, ColumnDelimeter:System.String, RowDelimeter:System.String, NullExpr:System.String):System.String (Method)
Index:System.String (Get Property)
Index:System.Void (Set Property)
let_ActiveConnection(pvar:System.Object):System.Void (Method)
let_Source(pvSource:System.String):System.Void (Method)
LockType:ADODB.LockTypeEnum (Get Property)
LockType:System.Void (Set Property)
MarshalOptions:ADODB.MarshalOptionsEnum (Get Property)
MarshalOptions:System.Void (Set Property)
MaxRecords:System.Int32 (Get Property)
MaxRecords:System.Void (Set Property)
Move(NumRecords:System.Int32, Start:System.Object):System.Void (Method)
MoveFirst():System.Void (Method)
MoveLast():System.Void (Method)
MoveNext():System.Void (Method)
MovePrevious():System.Void (Method)
NextRecordset(RecordsAffected:System.Object&):ADODB.Recordset (Method)
Open(Source:System.Object, ActiveConnection:System.Object, CursorType:ADODB.CursorTypeEnum, LockType:ADODB.LockTypeEnum, Options:System.Int32):System.Void (Method)
PageCount:System.Int32 (Get Property)
PageSize:System.Int32 (Get Property)
PageSize:System.Void (Set Property)
Properties:ADODB.Properties (Get Property)
RecordCount:System.Int32 (Get Property)
Requery(Options:System.Int32):System.Void (Method)
Resync(AffectRecords:ADODB.AffectEnum, ResyncValues:ADODB.ResyncEnum):System.Void (Method)
Save(Destination:System.Object, PersistFormat:ADODB.PersistFormatEnum):System.Void (Method)
Seek(KeyValues:System.Object, SeekOption:ADODB.SeekEnum):System.Void (Method)
Sort:System.String (Get Property)
Sort:System.Void (Set Property)
Source:System.Object (Get Property)
Source:System.Void (Set Property)
State:System.Int32 (Get Property)
Status:System.Int32 (Get Property)
StayInSync:System.Boolean (Get Property)
StayInSync:System.Void (Set Property)
Supports(CursorOptions:ADODB.CursorOptionEnum):System.Boolean (Method)
Update(Fields:System.Object, Values:System.Object):System.Void (Method)
UpdateBatch(AffectRecords:ADODB.AffectEnum):System.Void (Method)
使用您的代码稍微修改了示例:

Class Database  {
    [string] $databasePath
    $connection
    $adStateClosed

    Database($dbPath){
          $this.connectToDatabase($dbPath)
    }

    connectToDatabase($Db){
        $this.databasePath = $Db
        $this.connection = New-Object -ComObject ADODB.Connection
        $this.connection.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source='$Db'" )
        if($this.connection -eq $NULL -OR $this.connection.State -eq $this.adStateClosed){
            Throw "$this.DatabasePath found but a valid connection could not be made."
        }
    }
    [object] openRecordSet([string] $query, $cursorType, $lockType){
        $RecordSet = new-object -ComObject ADODB.Recordset
        $RecordSet.Open($query, $this.connection, $cursorType, $lockType)
        return $RecordSet
    }
}


$db = [Database]::new("C:\MonthlySalesReport\MonthlySalesReports.accdb")
$query = "SELECT 1 AS CONNECTED"
$result = $db.openRecordSet($query, 3, 1)

for ($i = 0; $i -lt $result.RecordCount; $i++) {
    $record = $result[$i]
    Write-Host ($record.Name + " " + $record.Value)
}

$query出错时的值、长度和索引是什么?$query=SELECT*FROM[Tbl_Users]其中[USERNAME]=john.doe长度是:61我不确定索引指的是什么,它只是一个普通字符串,不是数组或哈希表。您能发布填充$query变量的实际行吗?我确信您刚才是从内存中键入的,但它看起来不正确,因此我希望看到实际的行上面的行是从控制台复制和粘贴的,它是第一个代码块中Write Host语句的输出,尽管我在左侧添加了$以指示它是变量。这是从编辑器中复制和粘贴的实际代码行:$query=SELECT*from[Tbl_Users],其中[USERNAME]=`$val`此外,我刚刚仔细检查并确认$val确实是纯文本字符串,而不是数组或哈希表。
Class Database  {
    [string] $databasePath
    $connection
    $adStateClosed

    Database($dbPath){
          $this.connectToDatabase($dbPath)
    }

    connectToDatabase($Db){
        $this.databasePath = $Db
        $this.connection = New-Object -ComObject ADODB.Connection
        $this.connection.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source='$Db'" )
        if($this.connection -eq $NULL -OR $this.connection.State -eq $this.adStateClosed){
            Throw "$this.DatabasePath found but a valid connection could not be made."
        }
    }
    [object] openRecordSet([string] $query, $cursorType, $lockType){
        $RecordSet = new-object -ComObject ADODB.Recordset
        $RecordSet.Open($query, $this.connection, $cursorType, $lockType)
        return $RecordSet
    }
}


$db = [Database]::new("C:\MonthlySalesReport\MonthlySalesReports.accdb")
$query = "SELECT 1 AS CONNECTED"
$result = $db.openRecordSet($query, 3, 1)

for ($i = 0; $i -lt $result.RecordCount; $i++) {
    $record = $result[$i]
    Write-Host ($record.Name + " " + $record.Value)
}