Rmm
Rmm

Reputation: 85

Exporting query result to the excel sheet using powershell

I wrote a function Get-oracleresultDa which have oracle connection properties.Through which I can query my DB.

But,the problem is which I try to export the data to the excel sheet it only returns the result of the second query i.e)no status and not no type

$results = Get-OracleResultDa -conString $connectionString -sqlString $query 
-Verbose
$results | SELECT no, type| Export-CSV "H:\Book2.csv" -Force
$rows++
$results1 = Get-OracleResultDa -conString $connectionString -sqlString 
$created -Verbose
$results1 | SELECT no, status| Export-CSV "H:\Book2.csv" -
NoTypeInformation

The below mentioned block was in the first 10 linesof the script

$file="H:\Book2.csv"
$excel = New-Object -ComObject excel.application
#Makes Excel Visable
$excel.Application.Visible = $true
$excel.DisplayAlerts = $false
#Creates Excel workBook
$book = $excel.Workbooks.Add()
#Adds worksheets

#gets the work sheet and Names it
$sheet = $book.Worksheets.Item(1)
$sheet.name = 'Created'
#Select a worksheet
$sheet.Activate() | Out-Null

I have few more query's which also as to be exported

Upvotes: 0

Views: 938

Answers (1)

Mihail Kuznesov
Mihail Kuznesov

Reputation: 575

If you use powershell 3.0 or better, you can use -Append modificator

$results = Get-OracleResultDa -conString $connectionString -sqlString $query 
-Verbose
$results | SELECT no, type| Export-CSV "H:\Book2.csv" -Force
$rows++



$results1 = Get-OracleResultDa -conString $connectionString -sqlString 
$created -Verbose
$results1 | SELECT no, status| Export-CSV "H:\Book2.csv" -
NoTypeInformation -Append

Upvotes: 1

Related Questions