gpt4 book ai didi

powershell - 无法从一个函数返回一个对象并将其传递给另一个

转载 作者:行者123 更新时间:2023-12-05 00:15:08 27 4
gpt4 key购买 nike

我有一个 Powershell 函数,它调用一个存储过程并以对 ExecuteReader 的调用结束。 .这将返回一个对象,然后我将其传递给另一个函数。在该过程中,对象的类型似乎在某处发生了变化。我怀疑我无意中在某处调用了一个方法。

我已将脚本精简为:

Param(
[string] $DatabaseHost,
[int32] $RunA,
[int32] $RunB
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# This function works as expected.
Function New-DatabaseConnection {
Param(
[string] $databaseHost
)
$connectionProperties = @{}
$connectionProperties.ConnectionString = "Server=$databaseHost;Database=fitbit;Integrated Security=True"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection -Property $connectionProperties
$connection.Open()
return $connection
}

Function Invoke-StoredProcedure {
Param(
[int32] $runA,
[int32] $runB
)
$command = $connection.CreateCommand()
$command.CommandType = [System.Data.CommandType] 'StoredProcedure'
$command.CommandText = 'analysis.compareRunsWithSameInputs'
[void] $command.Parameters.Add('@runA', $runA)
[void] $command.Parameters.Add('@runB', $runB)
return $command.ExecuteReader() # What happens between here and the call to Write-ResultSetToSheet?
}

Function Write-ResultSetToSheet {
Param(
[System.Data.SqlClient.SqlDataReader] $reader
)
# The body of this function is irrelevant, because we don't get this far.
[void] $reader.Read()
Write-Output $reader.GetString(0)
}

$connection = New-DatabaseConnection $DatabaseHost
try {
$reader = Invoke-StoredProcedure $RunA $RunB
Write-ResultSetToSheet $reader # This fails, because somehow the type of $reader has changed from SqlDataReader to DataRecordInternal.
} finally {
$connection.Close()
}

当我执行此操作时,出现此错误:
Write-ResultSetToSheet : Cannot process argument transformation on parameter 'reader'. Cannot convert the "System.Data.Common.DataRecordInternal" value of type "System.Data.Common.DataRecordInternal" to type "System.Data.SqlClient.SqlDataReader".
At C:\dev\ps1\Invoke-SoTest.ps1:45 char:28
+ Write-ResultSetToSheet $reader
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Write-ResultSetToSheet], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Write-ResultSetToSheet

合并这两个函数虽然有效:
...
[void] $command.Parameters.Add('@runB', $runB)
$reader = $command.ExecuteReader()
Write-Output $reader.GetType() # I'm using this to check the type of the object. See below for more details.
[void] $reader.Read()
Write-Output $reader.GetString(0)

这是 Write-Output $reader.GetType() 的输出:
IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                              
-------- -------- ---- --------
True False SqlDataReader System.Data.Common.DbDataReader

(请注意,将 $reader 参数的声明类型从 System.Data.SqlClient.SqlDataReader 更改为 System.Data.Common.DbDataReader 没有帮助。)

我是一位经验丰富的开发人员,但不熟悉 .NET 和 PowerShell。

最佳答案

从函数返回时,Powershell 有时会尝试展开对象。要强制 Powershell 不展开对象,请在返回的变量前使用逗号,return ,$myVar
这应该有助于在函数之间移动对象。如果 Powershell 难以确定对象类型(例如, [int]$myInt = 7

另见 Powershell Operators了解更多信息。

关于powershell - 无法从一个函数返回一个对象并将其传递给另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com