PowerShell script exchange
Post Reply
Jack1874
Enthusiast
Posts: 95
Liked: 5 times
Joined: Oct 17, 2015 3:32 pm
Full Name: Stuart Little
Location: Canada
Contact:

Script to export and email custom attribute ...

Post by Jack1874 »

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: 95
Liked: 5 times
Joined: Oct 17, 2015 3:32 pm
Full Name: Stuart Little
Location: Canada
Contact:

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

Post by Jack1874 »

I have something like this ...

Code: Select all

Get-VM -Name *>.CustomFields
It gives me the output.. but not the guestname.

Thoughts??
nefes
Veeam Software
Posts: 643
Liked: 162 times
Joined: Dec 10, 2012 8:44 am
Full Name: Nikita Efes
Contact:

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

Post by nefes »

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>
Jack1874
Enthusiast
Posts: 95
Liked: 5 times
Joined: Oct 17, 2015 3:32 pm
Full Name: Stuart Little
Location: Canada
Contact:

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

Post by Jack1874 »

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: 95
Liked: 5 times
Joined: Oct 17, 2015 3:32 pm
Full Name: Stuart Little
Location: Canada
Contact:

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

Post by Jack1874 »

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: 95
Liked: 5 times
Joined: Oct 17, 2015 3:32 pm
Full Name: Stuart Little
Location: Canada
Contact:

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

Post by Jack1874 »

Jack1874 wrote:
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*>
nefes
Veeam Software
Posts: 643
Liked: 162 times
Joined: Dec 10, 2012 8:44 am
Full Name: Nikita Efes
Contact:

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

Post by nefes »

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
Jack1874
Enthusiast
Posts: 95
Liked: 5 times
Joined: Oct 17, 2015 3:32 pm
Full Name: Stuart Little
Location: Canada
Contact:

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

Post by Jack1874 »

Got it..thanks. It works great.

Is it possible to dump the data into a CSV .. and then email the CSV?
nefes
Veeam Software
Posts: 643
Liked: 162 times
Joined: Dec 10, 2012 8:44 am
Full Name: Nikita Efes
Contact:

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

Post by nefes » 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>
Jack1874
Enthusiast
Posts: 95
Liked: 5 times
Joined: Oct 17, 2015 3:32 pm
Full Name: Stuart Little
Location: Canada
Contact:

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

Post by Jack1874 »

Perfect.. all tested and works as required.

Thanks for you help.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 21 guests