• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 971
  • Last Modified:

Powershell Oracle Script to Send Email

Hi,

Currently I have the following script to run a query from Oracle:

$connectionString = "Data Source=DataSource;User Id=User;Password=Password;
Integrated Security=no"

$queryString = "Select Seq, Process from load_log"
[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
$command = new-Object System.Data.OracleClient.OracleCommand($queryString, $connection)
$connection.Open()
$resources = $command.ExecuteReader()
# Write out the results 
while ($resources.read()) {
    $Process=$resources.GetDecimal(0)
	$Status=$resources.GetString(1)
	Write-Host $Process $Status
	}
$connection.Close()

Open in new window


I will need send all the results from the while by email.

How can I print the while results into Variable and then read that variable out of the while?


Thank you for your help!
0
Gonzalo Becerra
Asked:
Gonzalo Becerra
  • 3
  • 2
2 Solutions
 
Krzysztof PytkoActive Directory EngineerCommented:
Have you tried to replace Write-Host cmd-let with variable ?

Instead of Write-Host $Process $Status use $toSend=$toSend+" "+$Process+" "+$Status

at the beginning of a script define empty $toSend variable using $toSend=""

Now you can use Write-Host $toSend out of while loop and use it to send an email

Regards,
Krzysztof
0
 
Gonzalo BecerraAuthor Commented:
Thank you is working like this, but I see all together in the results. I would like to see like

Row1  Row1
Row2  Row2


Currently I can see all together like:

Row1,Row1,Row2,Row2  without newline after each row.
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Oh, ok then you need to add return to beginning of new line and new line separators

Try this
$toSend=$toSend+" "+$Process+" "+$Status+"`r`n"

Note! ` character is this one below ESC key

Krzysztof
0
 
Gonzalo BecerraAuthor Commented:
Thank you very much! is working! :)
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Great! You're welcome :)
I'm glad I could help

Krzysztof
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now