Powershell Oracle Script to Send Email

Posted on 2012-09-05
Last Modified: 2012-09-05

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)
$resources = $command.ExecuteReader()
# Write out the results 
while ($ {
	Write-Host $Process $Status

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!
Question by:Gonzalo Becerra
    LVL 39

    Accepted Solution

    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

    LVL 1

    Author Comment

    by:Gonzalo Becerra
    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.
    LVL 39

    Assisted Solution

    by:Krzysztof Pytko
    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

    LVL 1

    Author Closing Comment

    by:Gonzalo Becerra
    Thank you very much! is working! :)
    LVL 39

    Expert Comment

    by:Krzysztof Pytko
    Great! You're welcome :)
    I'm glad I could help


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
    This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now