如何展示&在powershell中导出postgresql输出

时间:2017-02-14 04:35:34

标签: postgresql powershell scripting

如何在powershell中显示postgresql的输出。这是一个例子:

$query = "SELECT * FROM test where first_name='test'"

function Get-ODBC-Data{
   param([string]$query=$(throw 'query is required.'))
   $conn = New-Object System.Data.Odbc.OdbcConnection
   $conn.ConnectionString = "Driver={PostgreSQL Unicode(x64)};Server=localhost;Port=5432;Database=Test;Uid=test;Pwd=test;"
   $conn.open()
   $cmd = New-object System.Data.Odbc.OdbcCommand($query,$conn)
   $ds = New-Object system.Data.DataSet
   (New-Object system.Data.odbc.odbcDataAdapter($cmd)).fill($ds) | out-null
   $conn.close()
   $ds.Tables[0]
}

function Set-ODBC-Data{
   param([string]$query=$(throw 'query is required.'))
  $conn = New-Object System.Data.Odbc.OdbcConnection
  $conn.ConnectionString= "Driver={PostgreSQL Unicode(x64)};Server=localhost;Port=5432;Database=test;Uid=test;Pwd=test;"
  $cmd = new-object System.Data.Odbc.OdbcCommand($query,$conn)
  $conn.open()
  $cmd.ExecuteNonQuery()
  $conn.close()

}

$result = Get-ODBC-Data -query $query
$db = set-odbc-data -query $query
  1. 如何以屏幕截图中显示的格式显示或获取输出中的值?
  2. enter image description here

    1. 如何以正确的格式将输出导出到csv?

1 个答案:

答案 0 :(得分:0)

我没有测试它,但尝试使用以下命令作为powershell脚本的输出

C:\Get-Query | Out-GridView
C:\Get-Query | Export-CSV Info.csv
C:\Get-Query | Format-Table -autosize

假设名为Get-Query的文件中的脚本

The solution from this URL