2017-03-09 65 views
0

我的PowerShell腳本的偉大工程,返回1 DataTable來自不同數據庫的兩行,但我不能工作了如何將這些2行進行比較。比較結果從2個數據庫服務器下面

的情況是我有2臺數據庫服務器,並希望從同一個表上都比較max(id),比較,然後如果他們是不同的可能警報。

警報位,我可以做,但我以前沒有使用過DataTable秒。

function readServer1 { 
    # Connection variables 
    $server = "db1" 
    $port = 1234 
    $driver = "Adaptive Server Enterprise" 
    $query = "select max(id) as 'id' from table" 
    $db = "db" 
    $uid = "uid" 
    $pwd = "pwd" 
    # Create Object and Connection 
    $conn = New-Object System.Data.Odbc.OdbcConnection 
    $conn.ConnectionString = "driver={$driver};db=$db;na=$server,$port;uid=$uid;pwd=$pwd;" 
    $conn.Open() 
    $cmd = New-Object System.Data.Odbc.OdbcCommand($query, $conn) 
    $cmd.CommandTimeout = 30 
    # Create a Data Table 
    $dt = New-Object System.Data.DataTable 
    $dt.Load($cmd.ExecuteReader()) 
    $dt.Rows 
    # Close Connection 
    $conn.Close() 
} 

function readServer2 { 
    # Connection variables 
    $server = "db2" 
    $port = 1234 
    $driver = "Adaptive Server Enterprise" 
    $query = "select max(id) as 'id' from table" 
    $db = "db" 
    $uid = "uid" 
    $pwd = "pwd" 
    # Create Object and Connection 
    $conn = New-Object System.Data.Odbc.OdbcConnection 
    $conn.ConnectionString = "driver={$driver};db=$db;na=$server,$port;uid=$uid;pwd=$pwd;" 
    $conn.Open() 
    $cmd = New-Object System.Data.Odbc.OdbcCommand($query, $conn) 
    $cmd.CommandTimeout = 30 
    # Create a Data Table 
    $dt = New-Object System.Data.DataTable 
    $dt.Load($cmd.ExecuteReader()) 
    $dt.Rows 
    # Close Connection 
    $conn.Close() 
} 

readServer1 
readServer2 

它返回的結果是這樣的:

id 
-- 
12345 
12346 
+0

你想通過比較來實現什麼?如果你只是想知道,如果兩個結果不同,你可以這樣做:'如果($比較(readserver1)$(readserver2)){//不同}' – Paul

+0

我覺得我跑的方式進入1個表沒有似乎有助於比較方法。 – benji1304

回答

0

只需將功能執行結果變量並加以比較。像這樣:

$var1 = readServer1 | Select -Expand id 
$var2 = readServer2 | Select -Expand id 
if ($var1 -ne $var2) { Do-That } 
+0

似乎不喜歡,因爲當我將它們設置爲變量輸出爲'System.Data.DataRow' – benji1304

+0

@ benji1304,我已編輯我的答案。你會想要擴展屬性來獲取價值。 –