我有一个简单的函数来查找Oracle中的一些数据,并返回一个包含表中各种数据元素的对象。
function Get-OracleInfo {
[OutputType([PSObject])]
Param( [Parameter(Mandatory=$False)][string]$BindValue,
[Parameter(Mandatory=$True)][string]$OraclePath,
[Parameter(Mandatory=$True)][string]$OracledbConnectionString,)
Begin {
$Query="select stuff......"
Add-Type -Path $OraclePath
}
PROCESS {
#get data
try {
$connection=New-Object Oracle.ManagedDataAccess.Client.OracleConnection($OracledbConnectionString)
$connection.Open()
$OprIDLookupCmd=$connection.CreateCommand()
$OprIDLookupCmd.CommandText=$Query
#set bind value to parameter
$OprIDLookupBindParam = New-Object Oracle.ManagedDataAccess.Client.OracleParameter
$AccountLookupBindParam.Value = $BindValue
$AccountLookupCmd.Parameters.Add($AccountLookupBindParam)
$AccountRdr=$AccountLookupCmd.ExecuteReader()
if ($AccountRdr.Read()) {
$accountInfo = New-Object psobject -Property @{'StringVal1'=$AccountRdr.Item("field1")
'StringVal2'=$AccountRdr.Item("field2")
'Date1'=[datetime]$AccountRdr.Item("field3")}
}
}
catch
{
Write-Error ("Error in lookup Account - Can't open connection: {0}`n{1}" -f
$connection.ConnectionString, $_.Exception.ToString())
}
}
END {
if ($connection.State -eq 'Open') { $connection.close() }
return $accountInfo
}
它在Oracle中获取数据就好了,并将表字段分配给对象就好了。在调试中运行它时,查看变量$ accountInfo的值,它返回一个具有3个属性的对象,正如预期的那样。
String1 Date2 String2
-------- ------------ -----
StringVal2 5/11/2016 12:00:00 AM StringVal2
但是当从命令行调用函数并将其分配给变量.....从函数返回时,它显示为具有2个元素的数组。
$UserData=Get-OracleInfo -BindValue $account -OraclePath $OraclePath -OracledbConnectionString $OracledbConnectionString
$ UserData [1]是返回的对象(意味着它显示具有Stringval1,Stringval2,Date1的3个属性的对象)。我无法弄清楚它是作为一个数组返回的原因,第一个元素(即$ UserData [0])是一个OracleObject。以下是运行命令时的详细信息
$UserData | Get-Member
TypeName: Oracle.ManagedDataAccess.Client.OracleParameter
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLifetimeService()
ResetDbType Method void ResetDbType()
ResetOracleDbType Method void ResetOracleDbType()
ToString Method string ToString()
ArrayBindSize Property int[] ArrayBindSize {get;set;}
ArrayBindStatus Property Oracle.ManagedDataAccess.Client.OracleParameterStatus[] ArrayBindStatus {get;set;}
CollectionType Property Oracle.ManagedDataAccess.Client.OracleCollectionType CollectionType {get;set;}
DbType Property System.Data.DbType DbType {get;set;}
Direction Property System.Data.ParameterDirection Direction {get;set;}
IsNullable Property bool IsNullable {get;set;}
Offset Property int Offset {get;set;}
OracleDbType Property Oracle.ManagedDataAccess.Client.OracleDbType OracleDbType {get;set;}
OracleDbTypeEx Property Oracle.ManagedDataAccess.Client.OracleDbType OracleDbTypeEx {get;set;}
ParameterName Property string ParameterName {get;set;}
Precision Property byte Precision {get;set;}
Scale Property byte Scale {get;set;}
Size Property int Size {get;set;}
SourceColumn Property string SourceColumn {get;set;}
SourceColumnNullMapping Property bool SourceColumnNullMapping {get;set;}
SourceVersion Property System.Data.DataRowVersion SourceVersion {get;set;}
Status Property Oracle.ManagedDataAccess.Client.OracleParameterStatus Status {get;set;}
UdtTypeName Property string UdtTypeName {get;set;}
Value Property System.Object Value {get;set;}
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Date1 NoteProperty datetime Date1=5/11/2016 12:00:00 AM
StringVal2 NoteProperty string StringVal2=String2
StringVal1 NoteProperty string StringVal1=String1
希望我对事件和问题的解释是有道理的......我完全不知所措,所以感谢任何帮助。 谢谢!
答案 0 :(得分:2)
尝试抛弃添加操作的结果:
[void]$AccountLookupCmd.Parameters.Add($AccountLookupBindParam)