2014-01-22 129 views
0

我使用PowerShell來查詢列表從SQL數據庫

  1. 取IP地址從IP地址的列表
  2. 將其插入到SQL查詢地址
  3. 輸出結果

到目前爲止,該代碼僅適用於包含一個IP地址的情況。如果我包含多個腳本無限期掛起

此代碼似乎是正確的,應該修復哪些內容?

$file = Get-Content C:\list.txt 

$conn.Open() 

foreach ($k in $file){ 
    write-host $k 

    $sql = "SELECT dbo.sem_computer.COMPUTER_NAME,[IP_ADDR1_TEXT],(dbo.SEM_AGENT.FREE_DISK/1073741824)as 'Free Disk Space (GB)', 
    (dbo.SEM_COMPUTER.DISK_TOTAL/1073741824) as 'Total Disk Space (GB)', 
    (dbo.SEM_AGENT.FREE_MEM/1073741824) as 'Free Memory (GB)', (dbo.SEM_COMPUTER.MEMORY/1073741824) as 'Total Memory (GB)', 
    dbo.SEM_COMPUTER.OPERATION_SYSTEM,NAME, SEM_AGENT.MAJOR_VERSION 
    FROM dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.IDENTITY_MAP, dbo.SEM_CLIENT, dbo.SEM_AGENT 
    WHERE [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID 
    AND dbo.SEM_CLIENT.GROUP_ID = IDENTITY_MAP.ID 
    AND dbo.SEM_CLIENT.COMPUTER_ID = dbo.SEM_COMPUTER.COMPUTER_ID 
    AND dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID 
    AND [IP_ADDR1_TEXT] = '$k'" 

    $cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn) 

    do{ 
    try{ 
     $rdr = $cmd.ExecuteReader() 

     # Read Computer Name, Computer ID, IP address, Domain, and Operating System from the Database into a multidimensional array 

     while ($rdr.read()){ 
      $sql_output += ,@($rdr.GetValue(0), $rdr.GetValue(1), $rdr.GetValue(2), $rdr.GetValue(3), $rdr.GetValue(4), $rdr.GetValue(5), $rdr.GetValue(6), $rdr.GetValue(7), $rdr.GetValue(8)) 
     } 
     $transactionComplete = $true 
    } 
    catch{ 
     $transactionComplete = $false 
    } 
    }until ($transactionComplete) 
} 

Write-host $sql_output 

$conn.Close() 
+1

你有沒有考慮爲建設循環中的SQL連接?另外,如果寫入$ sql_output時出錯,您將永遠運行。對於故障排除,我會寫$ host來保證文本是正確的,然後花時間確保您正確使用讀寫器。 – websch01ar

+0

謝謝@ websch01ar你的解決方案的作品! – Glowie

+0

我已將評論添加爲答案。如果你不介意,是否只是SQL連接? – websch01ar

回答

1

顯然,這是答案(從評論):

你有沒有考慮爲建設循環中的SQL連接? 另外,如果在寫入$ sql_output時發生錯誤,您將永遠運行 。對於故障排除,我會寫主機$ SQL來確保 文本是正確的,然後把我的時間確保您與讀者正常工作

+0

通過將$ conn.Open()移動到for循環中,我在for循環中構建了SQL連接 – Glowie

1

我可能只是測試,如果這個工程第一:

$file = Get-Content C:\list.txt 

$conn.Open() 

foreach ($k in $file){ 
    write-host $k 

    $sql = "SELECT dbo.sem_computer.COMPUTER_NAME,[IP_ADDR1_TEXT],(dbo.SEM_AGENT.FREE_DISK/1073741824)as 'Free Disk Space (GB)', 
    (dbo.SEM_COMPUTER.DISK_TOTAL/1073741824) as 'Total Disk Space (GB)', 
    (dbo.SEM_AGENT.FREE_MEM/1073741824) as 'Free Memory (GB)', (dbo.SEM_COMPUTER.MEMORY/1073741824) as 'Total Memory (GB)', 
    dbo.SEM_COMPUTER.OPERATION_SYSTEM,NAME, SEM_AGENT.MAJOR_VERSION 
    FROM dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.IDENTITY_MAP, dbo.SEM_CLIENT, dbo.SEM_AGENT 
    WHERE [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID 
    AND dbo.SEM_CLIENT.GROUP_ID = IDENTITY_MAP.ID 
    AND dbo.SEM_CLIENT.COMPUTER_ID = dbo.SEM_COMPUTER.COMPUTER_ID 
    AND dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID 
    AND [IP_ADDR1_TEXT] = '$k'" 

    $command = $conn.CreateCommand() 
    $command.CommandText = $sql 
    $sql_output = $command.ExecuteReader() 
} 

Write-host $sql_output 

$conn.Close() 

然後加入錯誤處理(try/catch語句)之後。