user 9191
user 9191

Reputation: 747

Get results from shell and export to Excel

Running the following command to run a sql script

$InvokeParams = @{
 Server = 'test'
 Database = 'test2'
 Username = 'admin'
 Password = 'password'
 InputFile = 'business.sql'
}
Invoke-SqlCmd @InvokeParams

Get-Process | Export-CSV  C:\test1.csv –Show
Invoke-Item C:\test1.csv

Once the script is done, for example :

TodayDate : 6/24/2019 12:00:00 AM
PrevDate : 6/23/2019 12:00:00 AM
Count : 100,000

TodayDate : 6/24/2019 12:00:00 AM
PrevDate : 6/23/2019 12:00:00 AM
Count : 200,000

shell grabs the output and send the data to test1.csv . TodayDate/PrevDate and Count should be set as columns on test1.csv file

Csv file does not show the output from shellscript - showing all the activities happening at the desktop

The same script will run for different databases

Upvotes: 0

Views: 701

Answers (2)

Maximilian Burszley
Maximilian Burszley

Reputation: 19704

In your code, you're capturing the currently running processes in your csv. If you want the return of your sql command captured, you need to either pipe from it, or capture it in a variable:

<# ... #>
$out = Invoke-SqlCmd @InvokeParams

$path = 'C:\test1.csv'
$out | Export-Csv -Path $path
Invoke-Item -Path $path

Upvotes: 1

jslipknot
jslipknot

Reputation: 435

I think you have to download/install the module first according to the documentation, which means:

Install-Module -Name ImportExcel -RequiredVersion 5.4.0

Upvotes: 1

Related Questions