reko_3 发表于 2018-9-2 08:37:34

Powershell 查询SQL数据库资料

function Get-DatabaseData {  
   
  
    param (
  
      $connectionString,
  
      $query,
  
      $isSQLServer
  
    )
  
    if ($isSQLServer) {
  
      Write-Verbose 'in SQL Server mode'
  
      $connection = New-Object -TypeName `
  
            System.Data.SqlClient.SqlConnection
  
    } else {
  
      Write-Verbose 'in OleDB mode'
  
      $connection = New-Object -TypeName `
  
            System.Data.OleDb.OleDbConnection
  
    }
  
    $connection.ConnectionString = $connectionString
  
    $command = $connection.CreateCommand()
  
    $command.CommandText = $query
  
    if ($isSQLServer) {
  
      $adapter = New-Object -TypeName `
  
      System.Data.SqlClient.SqlDataAdapter $command
  
    } else {
  
      $adapter = New-Object -TypeName `
  
      System.Data.OleDb.OleDbDataAdapter $command
  
    }
  
    $dataset = New-Object -TypeName System.Data.DataSet
  
    $adapter.Fill($dataset)
  
    $dataset.Tables
  
    $connection.close()
  
}
  

  

  
function Invoke-DatabaseQuery {
  
    [CmdletBinding(SupportsShouldProcess=$True,
  
                   ConfirmImpact='Low')]
  
    param (
  
      $connectionString,
  
      $query,
  
      $isSQLServer
  
    )
  
    if ($isSQLServer) {
  
      Write-Verbose 'in SQL Server mode'
  
      $connection = New-Object -TypeName `
  
            System.Data.SqlClient.SqlConnection
  
    } else {
  
      Write-Verbose 'in OleDB mode'
  
      $connection = New-Object -TypeName `
  
            System.Data.OleDb.OleDbConnection
  
    }
  
    $connection.ConnectionString = $connectionString
  
    $command = $connection.CreateCommand()
  
    $command.CommandText = $query
  
    if ($pscmdlet.shouldprocess($query)) {
  
      $connection.Open()
  
      $command.ExecuteNonQuery()
  
      $connection.close()
  
    }
  
}


页: [1]
查看完整版本: Powershell 查询SQL数据库资料