none
如何将WMI获得的数据通过ODBC的Connection方法写入数据库 RRS feed

  • 问题

  •     #Enumerate specific OU computers.
        Function List-Hosts
        {
         $ou = [ADSI]"LDAP://OU=WorkStations,DC=aiagz,DC=aiacr"
             foreach ($child in $ou.psbase.Children)
                {
                 if ($child.ObjectCategory -like '*computer*')
           {
            $child.Name
                 }
                 }
        }

        #Detect whethere online by ping CMD.
        Function IsAlive($strTarget)
        { 
             if (((ping -n 2 -w 1000 $strTarget|Out-String).ToLower()).contains("reply from"))
             {
                 return $true
             }
             else
             {
                 return $false
             }
       
        }

        #obtain installed software list by thru WMI win32_Product class.
        Function Get-InstalledSoftware($ComputerName)
        {
         Get-WmiObject win32_Product -ComputerName $ComputerName |where {$_.Name -NOTmatch "^Microsoft*"} |select Name
        }
       
       
        #Write recorder  to Database thru ODBC.
        Function Write-Records($strTarget,$InstallLists)
        {
     
           foreach($soft in $Lists)
           {
                 $Lists = $InstallLists
                $connection = New-Object -ComObject ADODB.Connection _
                   $strConn = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;User=sa;Password=abcd@1234;Option=3;"
                   $connection.Open($strConn)
                         $command = "INSERT INTO software(StrComputer,Name) VALUES _('$strTarget','$soft')"
          $connection.Execute($command)
                  }         
        }

     
        $strTarget = List-Hosts
       
        foreach($pc in $strTarget)
        {
       
         if(IsAlive($pc))
         {
          Write-Host $pc "Begin to collect installed software infomatin."
          $softs = Get-InstalledSoftware($pc)
          Write-Records($pc,$softs)
          }
         else
         {
            }
        }

    ----------------------------------------------------------------------------------------------------------------------------
        #Error occure Here ,Insert into INSERT INTO software('$strTarget','$soft')",But
        #thru DEBUG i find out in fact the SQL transform to \
        #INSERT INTO software(StrComputer,Name) VALUES('2009IT006 System.Object[]','')   
        #So I want to know have a solution that design a function that accepe a collection
        # from another function and then write to DB .
       
        # thanks.

    2012年12月9日 2:18

答案

全部回复