Powershell访问SQL Server数据库查询返回结果集

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

    function Invoke-SQL { 
      param( 
      [Parameter(Mandatory=$true)] 
      $sql = "select * from Kunden", 
      [Parameter(Mandatory=$true)] 
      $connectionstring 
      ) 
     
      $db = New-Object -comObject ADODB.Connection 
      $db.Open($connectionstring) 
      $rs = $db.Execute($sql) 
      while (!$rs.EOF) { 
        $hash = @{} 
        foreach ($field in $rs.Fields) { 
          $hash.$($field.Name) = $field.Value 
        } 
        $rs.MoveNext()     
        New-Object PSObject -property $hash 
      } 
      $rs.Close() 
      $db.Close() 
    }