2012-07-28 162 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

相關問題