tags:

views:

40

answers:

2

Hey, sorry for my bad english... i need to execute some periodic console applications that give me some results (on the console)... How can I execute it and have its return data sent to my email? I tried to use [Diagnostics.Process]::Start() and it launches my application but i don't know how to get the return... I do not want the exitCode, I want the text that the application prints on screen. Using PS V2 CTP3.

* UPDATE

The solutions presented worked fine but i have a problem... this application that i need to execute is gfix from the firebird database and now i discovered a thing, I can't redirect the output of gfix to a file, if i execute on command prompt the line:

gfix.exe -v -f dabatase.gdb > c:\test.txt

it print the output on the screen and the file is empty. Same thing if i try to assign it to a variable... i don't know what difference gfix has from the other console apps that i use, but looks like its output can't be redirected...

Has someone seeing this?

* UPDATE 2

Even if i use Start-transcript /Stop-Transcript, although on the screen i see the gfix output, on the file there is only the commands :/

* UPDATE 3

Found the solution here http://edn.embarcadero.com/br/article/25605

+2  A: 

Something like this could work:

# temporary file
$f = [io.path]::GetTempFileName()
# start process and redirect its output to the temp file
ping localhost > $f

# create and send email
$emailFrom = "[email protected]"
$emailTo = "[email protected]"
$subject = "results"
$body = (Get-Content $f) -join "`r`n"
$smtpServer = "your smtp server"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

# delete the file
remove-item $f

I think in this case [Diagnostics.Process]::Start() is not needed. Besides that there is a cmdlet Start-Process that does almost the same.

stej
+3  A: 

PowerShell v2 is now out, so you could consider upgrading.

Then, you can simply try: PS > [string]$ipconfig=ipconfig PS > send-mailmessage -to some_email -from from_email -subject PowerShell -body $ipconfig -bodyashtml -smtp my_smtp_server

Now, it depends on how complicated your command-line output is, because the above method will collapse multiple lines into a single on.

Marco Shaw
You are right, assignment works as well. So in case that there are more lines, this helps: `$ipconfig = (ipconfig) -join "`r`n"`
stej