我使用PowerShell來查詢列表從SQL數據庫
- 取IP地址從IP地址的列表
- 將其插入到SQL查詢地址
- 輸出結果
到目前爲止,該代碼僅適用於包含一個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()
你有沒有考慮爲建設循環中的SQL連接?另外,如果寫入$ sql_output時出錯,您將永遠運行。對於故障排除,我會寫$ host來保證文本是正確的,然後花時間確保您正確使用讀寫器。 – websch01ar
謝謝@ websch01ar你的解決方案的作品! – Glowie
我已將評論添加爲答案。如果你不介意,是否只是SQL連接? – websch01ar