Powershell V2将查询返回的值分配给变量返回NULL

时间:2012-07-28 03:00:44

标签: powershell odbc

我认为我做错了什么,我试图从Powershell 2查询Postgres数据库,使用ODBC,如果我使用PGADMIN(Postgres GUI)查询,DB会返回这样的字符串:

0 / 164DAAB0

这是一个HEX,开头有两个额外的字符。 我需要提取HEX。

当我这样做并将其分配给变量时,我打印变量,我什么都没得到,空了。 我尝试使用Powershell V3的计算机,它运行得很好,但我需要让它在PS2上运行。 我希望有人发现我的问题,我认为有一个不同/更好的方法来做到这一点,所以我的变量获取查询返回的字符串。

代码是:

$MasterDBIP = "172.16.50.20";
$MasterDBPort = "5432";
$MasterDB = "database1";
$MasterUid = "postgres";
$MasterPassword = "postgres";
$MasterMasterDBConnectionString = "Driver={PostgreSQL Unicode(x64)};Server=$MasterDBIP;Port=$MasterDBPort;Database=$MasterDB;Uid=$MasterUid;Pwd=$MasterPassword;"
$MasterDBConn = New-Object System.Data.Odbc.OdbcConnection
$MasterDBConn.ConnectionString = $MasterMasterDBConnectionString;
$MasterDBConn.Open();
$MasterDBCmdCurr = $MasterDBConn.CreateCommand();
$MasterDBCmdCurr.CommandText = "SELECT pg_last_xlog_receive_location();";
$MasterDBResultCurr = $MasterDBCmdCurr.ExecuteReader();
$MasterUserTableCurr=New-Object system.data.datatable
$MasterUserTableCurr.load($MasterDBResultCurr)
[string]$MasterStringValueCurr = $MasterUserTableCurr.pg_last_xlog_receive_location;
$MasterDBConn.Close();
$MasterStringValueCurr;
exit (0)

附加信息:

命令$ MasterUserTableCurr |获得会员 返回:

   TypeName: System.Data.DataRow

Name                          MemberType            Definition
----                          ----------            ----------
AcceptChanges                 Method                System.Void AcceptChanges()
BeginEdit                     Method                System.Void BeginEdit()
CancelEdit                    Method                System.Void CancelEdit()
ClearErrors                   Method                System.Void ClearErrors()
Delete                        Method                System.Void Delete()
EndEdit                       Method                System.Void EndEdit()
Equals                        Method                bool Equals(System.Object obj)
GetChildRows                  Method                System.Data.DataRow[] GetChildRows(string relationName), System.Data.DataRow[] GetChildRows(string relat...
GetColumnError                Method                string GetColumnError(int columnIndex), string GetColumnError(string columnName), string GetColumnError(...
GetColumnsInError             Method                System.Data.DataColumn[] GetColumnsInError()
GetHashCode                   Method                int GetHashCode()
GetParentRow                  Method                System.Data.DataRow GetParentRow(string relationName), System.Data.DataRow GetParentRow(string relationN...
GetParentRows                 Method                System.Data.DataRow[] GetParentRows(string relationName), System.Data.DataRow[] GetParentRows(string rel...
GetType                       Method                type GetType()
HasVersion                    Method                bool HasVersion(System.Data.DataRowVersion version)
IsNull                        Method                bool IsNull(int columnIndex), bool IsNull(string columnName), bool IsNull(System.Data.DataColumn column)...
RejectChanges                 Method                System.Void RejectChanges()
SetAdded                      Method                System.Void SetAdded()
SetColumnError                Method                System.Void SetColumnError(int columnIndex, string error), System.Void SetColumnError(string columnName,...
SetModified                   Method                System.Void SetModified()
SetParentRow                  Method                System.Void SetParentRow(System.Data.DataRow parentRow), System.Void SetParentRow(System.Data.DataRow pa...
ToString                      Method                string ToString()
Item                          ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string columnName) {get;set;}, System...
pg_last_xlog_receive_location Property              System.String pg_last_xlog_receive_location {get;}

1 个答案:

答案 0 :(得分:0)

我没有那种环境,所以很难回答,但无论如何我都会尝试。只是为了澄清,当你执行:

$MasterUserTableCurr.pg_last_xlog_receive_location 

你回来了:0 / 164DAAB0,你之后的值应该是:64DAAB0,对吧?

可以粘贴这些命令的结果吗?

$MasterUserTableCurr | Get-Member

$MasterUserTableCurr.pg_last_xlog_receive_location | Get-Member