Pass Get-VBRBackupSession results to Function

PowerShell script exchange

Pass Get-VBRBackupSession results to Function

Veeam Logoby user0000001 » Fri Nov 15, 2013 1:56 pm

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
user0000001
Lurker
 
Posts: 2
Liked: never
Joined: Fri Nov 15, 2013 12:56 pm
Full Name: user user

Re: Pass Get-VBRBackupSession results to Function

Veeam Logoby nefes » Fri Nov 15, 2013 2:23 pm 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
nefes
Veeam Software
 
Posts: 554
Liked: 129 times
Joined: Mon Dec 10, 2012 8:44 am
Full Name: Nikita Efes

Re: Pass Get-VBRBackupSession results to Function

Veeam Logoby tsightler » Fri Nov 15, 2013 3:23 pm 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
tsightler
Veeam Software
 
Posts: 4890
Liked: 1830 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Pass Get-VBRBackupSession results to Function

Veeam Logoby user0000001 » Fri Nov 15, 2013 5:00 pm

Thanks, both options works!

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

Thanks!

Really Thanks!
user0000001
Lurker
 
Posts: 2
Liked: never
Joined: Fri Nov 15, 2013 12:56 pm
Full Name: user user


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 1 guest