尝试从文件中读取IP地址,将其插入SQL语句,然后执行并输出语句。
但是,只有第一个IP地址被插入SQL语句并被执行。
这是代码
$conn.Open()
foreach ($k in $file_line){
$SQL = "SELECT dbo.sem_computer.COMPUTER_NAME,
[IP_ADDR1_TEXT],dbo.sem_computer.COMPUTER_DOMAIN_NAME, dbo.SEM_COMPUTER.OPERATION_SYSTEM,NAME,dbo.SEM_AGENT.AGENT_VERSION,
Convert(VARCHAR(12),DBO.PATTERN.PATTERNDATE,107) as Antivirus_Date
FROM dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.IDENTITY_MAP, dbo.SEM_CLIENT,dbo.SEM_AGENT,dbo.PATTERN
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 = SEM_COMPUTER.COMPUTER_ID
AND SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
AND dbo.PATTERN.PATTERN_IDX = dbo.SEM_AGENT.PATTERN_IDX
AND [IP_ADDR1_TEXT] = '$k'"
write-host $SQL
$cmd = New-Object System.Data.SqlClient.SqlCommand($SQL,$conn)
do{
try{
$rdr = $cmd.ExecuteReader()
# Read Computer Name, Computer ID and IP address 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))
write-host $sql_output
}
$transactionComplete = $true
}
catch{
$transactionComplete = $false
}
}until ($transactionComplete)
}
$conn.Close()
这是输出
SELECT dbo.sem_computer.COMPUTER_NAME,
[IP_ADDR1_TEXT],dbo.sem_computer.COMPUTER_DOMAIN_NAME, dbo.SEM_COMPUTER.OPERATION_SYSTEM,NAME,dbo.SEM_AGENT.AGENT_VERSION,
Convert(VARCHAR(12),DBO.PATTERN.PATTERNDATE,107) as Antivirus_Date
FROM dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.IDENTITY_MAP, dbo.SEM_CLIENT,dbo.SEM_AGENT,dbo.PATTERN
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 = SEM_COMPUTER.COMPUTER_ID
AND SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
AND dbo.PATTERN.PATTERN_IDX = dbo.SEM_AGENT.PATTERN_IDX
AND [IP_ADDR1_TEXT] = '10.10.10.1'
H2UA1050K63 10.10.10.1 WORKGROUP Windows 7 Enterprise Edition Group Version Jan 05, 2015
SELECT dbo.sem_computer.COMPUTER_NAME,
[IP_ADDR1_TEXT],dbo.sem_computer.COMPUTER_DOMAIN_NAME, dbo.SEM_COMPUTER.OPERATION_SYSTEM,NAME,dbo.SEM_AGENT.AGENT_VERSION,
Convert(VARCHAR(12),DBO.PATTERN.PATTERNDATE,107) as Antivirus_Date
FROM dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.IDENTITY_MAP, dbo.SEM_CLIENT,dbo.SEM_AGENT,dbo.PATTERN
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 = SEM_COMPUTER.COMPUTER_ID
AND SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
AND dbo.PATTERN.PATTERN_IDX = dbo.SEM_AGENT.PATTERN_IDX
AND [IP_ADDR1_TEXT] = '10.10.10.2'
代码看起来正确,为什么它会停止执行并使PowerShell挂起?
答案 0 :(得分:1)
导致挂起的可能原因是,执行生成异常的SQL语句出错会在异常处理程序中将$ transactionComplete设置为false。只要该异常继续发生,循环将继续执行。
假设$ cmd.ExecuteReader()抛出异常,那么你就不会得到任何输出。
尝试添加一些代码来输出catch块中的内容,看看是否有任何有用的东西。