2012-07-28 143 views
1

我认为我做错了什么,我试图从Powershell 2查询Postgres数据库,使用ODBC,数据库返回像这样的字符串,如果我使用PGADMIN查询(Postgres的GUI):Powershell V2将查询返回值分配给一个变量返回NULL

0/164DAAB0

那是与在开始时两个额外的字符的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) 

Additiional信息:

命令$ 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;} 

回答

0

我没有那个环境,所以很难回答,但我会想试试。只是为了澄清,当你执行:

$MasterUserTableCurr.pg_last_xlog_receive_location 

你回来:0/164DAAB0和价值,你';再之后应该是:64DAAB0,对不对?

你能粘贴这些命令的结果吗?

$MasterUserTableCurr | Get-Member 

而且

$MasterUserTableCurr.pg_last_xlog_receive_location | Get-Member 
+0

不完全,,,如果我运行查询SELECT pg_last_xlog_receive_location();用我的GUI,我得到了这样的东西:0/164DAAB0 ,,所以我试图从Powershell做同样的事情,我需要在变量内的值,所以'后来'我去掉前两个字符,并获得十六进制。我有问题分配从查询返回的值到变量中。我期望输入:$ MasterStringValueCurr;并得到字符串:0/164DAAB0,但我什么都没有(空白,空白),,没有错误。 – Dotty 2012-07-28 13:22:15

+0

我在原帖中添加了您请求的其他信息。 返回的最后一条命令: Get-Member:没有对象指定给get-member cmdlet。 – Dotty 2012-07-28 13:27:10

+0

你能用这个得到价值吗? $ MasterDBResultCurr = $ MasterDBCmdCurr.ExecuteReader(); while($ MasterDBResultCurr.Read()){$ MasterDBResultCurr [“pg_last_xlog_receive_location”]} – 2012-07-28 17:38:40