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!
LVL 1
Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAsked:
Who is Participating?
 
Krzysztof PytkoSenior Active 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 BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor 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 PytkoSenior Active 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 BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
Thank you very much! is working! :)
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Great! You're welcome :)
I'm glad I could help

Krzysztof
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.