查询SQL数据库中的IP地址列表

时间:2014-01-22 18:56:35

标签: sql powershell

我正在使用powershell

  1. 从IP地址列表中获取IP地址
  2. 将其插入SQL查询
  3. 输出结果
  4. 到目前为止,代码仅在我包含一个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()  
    

2 个答案:

答案 0 :(得分: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)。

答案 1 :(得分:1)

显然这是答案(来自评论):

  

您是否考虑过在for循环中构建Sql连接?   此外,如果写入$ sql_output时出错,您将运行   永远。为了排除故障,我会写主机$ sql来确保   文字是正确的,然后花时间确保你工作   与读者正确