PowerShell script exchange
Post Reply
user0000001
Lurker
Posts: 2
Liked: never
Joined: Nov 15, 2013 12:56 pm
Full Name: user user
Contact:

Pass Get-VBRBackupSession results to Function

Post by user0000001 »

How to I pass the results of this command to an function?

Code: Select all

$body = Get-VBRBackupSession |select JobName, Name, Result, CreationTime, EndTime |  where { $_.EndTime -gt $b} | where { $_.Result -eq "Success"} 
Currently I get System.Object[] or Veeam.Backup.Core.CBackupSession.

My work around is to attach a csv file to the email but I want the results in the email.


Code: Select all

Function SendEmail($body)
{
     Write-Host "Sending Email" 


     #SMTP server name
     $smtpServer = "server"
 
     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage
 
     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)
 
     #Email structure 
     $msg.From = "veeamscript@email.com"
     #$msg.ReplyTo = "replyto@xxxx.com"
     $msg.To.Add("scripts@email.com")
     $msg.subject = "Veeam Successfull Jobs 1"
     $msg.Attachments.Add("C:\Users\user\Documents\Test1.csv")

     $msg.body = $body
    
     #Sending email 
     $smtp.Send($msg)
     }
#End Function 




#gets the past 24 hours
$a = (get-date).adddays(-1)
#put date in correct format
$b = $a.tostring("MM/dd/yyyy HH:mm tt")

#the veeam command
$body = Get-VBRBackupSession |select JobName, Name, Result, CreationTime, EndTime |  where { $_.EndTime -gt $b} | where { $_.Result -eq "Success"} 
#workaround
Get-VBRBackupSession |select JobName, Name, Result, CreationTime, EndTime |  where { $_.EndTime -gt $b} | where { $_.Result -eq "Success"} | Export-CSV Test1.csv -notype

SendEmail($body)

4
nefes
Veeam Software
Posts: 649
Liked: 170 times
Joined: Dec 10, 2012 8:44 am
Full Name: Nikita Efes
Contact:

Re: Pass Get-VBRBackupSession results to Function

Post by nefes » 1 person likes this post

You can utilize Out-File cmdlet and use that file as email body:

Code: Select all

Get-VBRBackupSession |select JobName, Name, Result, CreationTime, EndTime |  where { $_.EndTime -gt $b} | where { $_.Result -eq "Success"} |Out-File "C:\body.txt"
$msg.body = Get-Content "C:\body.txt"
You could also format output before sending it to file, for example, with Format-Table or Format-List
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Pass Get-VBRBackupSession results to Function

Post by tsightler » 2 people like this post

Or if you like the general format, a simple Out-String

Code: Select all

$body = Get-VBRBackupSession |select JobName, Name, Result, CreationTime, EndTime |  where { $_.EndTime -gt $b} | where { $_.Result -eq "Success"} | Out-String
user0000001
Lurker
Posts: 2
Liked: never
Joined: Nov 15, 2013 12:56 pm
Full Name: user user
Contact:

Re: Pass Get-VBRBackupSession results to Function

Post by user0000001 »

Thanks, both options works!

I like the out-string as it has better formatting.

Thanks!

Really Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests