Script to export and email custom attribute ...

PowerShell script exchange

Script to export and email custom attribute ...

Veeam Logoby Jack1874 » Thu Jan 28, 2016 3:45 pm

Guys, have custom attribute in vCenter that we use to write to if the backup was successful.

We'd like to export the contents of that list to a CSV and email it out to our Ops guys daily.

Does anyone know of how we can do that...

TIA
Jack1874
Enthusiast
 
Posts: 88
Liked: 4 times
Joined: Sat Oct 17, 2015 3:32 pm
Location: Canada
Full Name: Stuart Little

Re: Script to export and email custom attribute ...

Veeam Logoby Jack1874 » Thu Jan 28, 2016 4:16 pm

I have something like this ...

Code: Select all
Get-VM -Name *>.CustomFields


It gives me the output.. but not the guestname.

Thoughts??
Jack1874
Enthusiast
 
Posts: 88
Liked: 4 times
Joined: Sat Oct 17, 2015 3:32 pm
Location: Canada
Full Name: Stuart Little

Re: Script to export and email custom attribute ...

Veeam Logoby nefes » Thu Jan 28, 2016 4:22 pm

It is doable with VMware PowerCLI, I've created some short sample to start with:
Code: Select all
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer <servername> -user <username> -password <pwd>
$vms = Get-VM -Name <*mask*>
$annotations = @{}
foreach ($vm in $vms) {$annotations[$vm] = (Get-Annotation -CustomAttribute <name of attribute> -Entity $vm).Value}
Send-MailMessage -Body ($annotations | Out-String) -From <backupadmin> -SmtpServer <yourserver.com> -Subject "B&R result" -To <address list>
nefes
Veeam Software
 
Posts: 534
Liked: 125 times
Joined: Mon Dec 10, 2012 8:44 am
Full Name: Nikita Efes

Re: Script to export and email custom attribute ...

Veeam Logoby Jack1874 » Thu Jan 28, 2016 4:28 pm

After a bit of searching. I got what was looking for...

Code: Select all
$Report = @()

Get-VM | foreach{

    $Summary = "" | Select VM

    $Summary.VM = $_.Name

    $_ | Get-Annotation | foreach{

        Add-Member -InputObject $Summary -MemberType NoteProperty -Name $_.Name -Value $_.Value

    }

    $Report += $Summary

}

$Report | Export-Csv -Path Annotations.csv -Delimiter ";" -NoTypeInformation
Jack1874
Enthusiast
 
Posts: 88
Liked: 4 times
Joined: Sat Oct 17, 2015 3:32 pm
Location: Canada
Full Name: Stuart Little

Re: Script to export and email custom attribute ...

Veeam Logoby Jack1874 » Thu Jan 28, 2016 4:28 pm

nefes wrote:It is doable with VMware PowerCLI, I've created some short sample to start with:
Code: Select all
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer <servername> -user <username> -password <pwd>
$vms = Get-VM -Name <*mask*>
$annotations = @{}
foreach ($vm in $vms) {$annotations[$vm] = (Get-Annotation -CustomAttribute <name of attribute> -Entity $vm).Value}
Send-MailMessage -Body ($annotations | Out-String) -From <backupadmin> -SmtpServer <yourserver.com> -Subject "B&R result" -To <address list>



Thank you sir... I'll try this too.
Jack1874
Enthusiast
 
Posts: 88
Liked: 4 times
Joined: Sat Oct 17, 2015 3:32 pm
Location: Canada
Full Name: Stuart Little

Re: Script to export and email custom attribute ...

Veeam Logoby Jack1874 » Thu Jan 28, 2016 4:33 pm

Jack1874 wrote:
nefes wrote:It is doable with VMware PowerCLI, I've created some short sample to start with:
Code: Select all
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer <servername> -user <username> -password <pwd>
$vms = Get-VM -Name <*mask*>
$annotations = @{}
foreach ($vm in $vms) {$annotations[$vm] = (Get-Annotation -CustomAttribute <name of attribute> -Entity $vm).Value}
Send-MailMessage -Body ($annotations | Out-String) -From <backupadmin> -SmtpServer <yourserver.com> -Subject "B&R result" -To <address list>



Thank you sir... I'll try this too.


What value to do I put for .. as I want to search all guests ?

Code: Select all
$vms = Get-VM -Name <*mask*>
Jack1874
Enthusiast
 
Posts: 88
Liked: 4 times
Joined: Sat Oct 17, 2015 3:32 pm
Location: Canada
Full Name: Stuart Little

Re: Script to export and email custom attribute ...

Veeam Logoby nefes » Thu Jan 28, 2016 4:38 pm

You can use get-vm without parameters if you want all vms from connected host.
As for guest name, you can use $vm.guest.hostname
nefes
Veeam Software
 
Posts: 534
Liked: 125 times
Joined: Mon Dec 10, 2012 8:44 am
Full Name: Nikita Efes

Re: Script to export and email custom attribute ...

Veeam Logoby Jack1874 » Thu Jan 28, 2016 5:07 pm

Got it..thanks. It works great.

Is it possible to dump the data into a CSV .. and then email the CSV?
Jack1874
Enthusiast
 
Posts: 88
Liked: 4 times
Joined: Sat Oct 17, 2015 3:32 pm
Location: Canada
Full Name: Stuart Little

Re: Script to export and email custom attribute ...

Veeam Logoby nefes » Thu Jan 28, 2016 5:29 pm 1 person likes this post

Yes, I've modified getting annotations and exporting to CSV, so you can see guest name and backup result in csv:
Code: Select all
$annotations = @{}
foreach ($vm in $vms) {$annotations[$vm.guest.hostname] = (Get-Annotation -CustomAttribute <name of attribute> -Entity $vm).Value}
$annotations.GetEnumerator() | Select-Object -Property @{n= 'Guest Name'; e = {$_.Name}}, @{n ='BackupResult'; e = {$_.Value}} | Export-Csv -Path <temp csv path> -NoTypeInformation
Send-MailMessage -Body ("see attach") -From <> -SmtpServer <> -Subject "B&R result" -To <> -Attachments <temp csv path>
nefes
Veeam Software
 
Posts: 534
Liked: 125 times
Joined: Mon Dec 10, 2012 8:44 am
Full Name: Nikita Efes

Re: Script to export and email custom attribute ...

Veeam Logoby Jack1874 » Fri Jan 29, 2016 5:06 pm

Perfect.. all tested and works as required.

Thanks for you help.
Jack1874
Enthusiast
 
Posts: 88
Liked: 4 times
Joined: Sat Oct 17, 2015 3:32 pm
Location: Canada
Full Name: Stuart Little


Return to PowerShell



Who is online

Users browsing this forum: Bing [Bot] and 8 guests