PowerShell script exchange
Post Reply
alphonsodavies
Novice
Posts: 6
Liked: never
Joined: Feb 18, 2022 3:16 pm
Full Name: Alphone Davies
Contact:

Export backup job status to .csv

Post by alphonsodavies »

hello Everyone,

pretty new to powershell as well as vmware and veeam backup. used this script to get backup information for our vms however, having difficulty exporting the results to .csv with following fields. vmname,backup_status,replication_status

any help/pointer is much appreciated.

Code: Select all

# Configuration
# vCenter server
$vcenter = "<vcenter_server>"
# To Exclude VMs from report add VM names to be excluded as follows
# $excludevms=@("vm1","vm2")
$excludevms=@()

# Connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
Connect-ViServer $vcenter | out-null

# Build hash table with excluded VMs
$excludedvms=@{}
foreach ($vm in $excludevms) {
    $excludedvms.Add($vm, "Excluded")
}

# Get a list of all VMs from vCenter and add to hash table, assume Unprotected
$vms=@{}
foreach ($vm in (Get-VM | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}}))  {
    if (!$excludedvms.ContainsKey($vm.VMname)) {
        $vms.Add($vm.VMname, "Unprotected")
    }
}

# Find all backup job sessions that have ended in the last 24 hours
$vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).addhours(-24)}

# Find all successfully backed up VMs in selected sessions (i.e. VMs not ending in failure) and update status to "Protected"
$backedupvms=@{}
foreach ($session in $vbrsessions) {
    foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
        if($vms.ContainsKey($vm.VMname)) {
            $vms[$vm.VMname]="Protected"
        }
    }
}

# Output VMs in color coded format based on status.
foreach ($vm in $vms.Keys)
{
  if ($vms[$vm] -eq "Protected") {
      write-host -foregroundcolor green "$vm is backed up"
  } else {
      write-host -foregroundcolor red "$vm is NOT backed up"
  }
}
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Export backup job status to .csv

Post by david.domask »

Hey Alphone,

Excuse the incoming wall of text please, but just want to share what I've battled with and explain why this isn't working :)

I'm not connected to my lab at the moment, but what's the issue you're having with exporting to CSV?

As I see you're using hash tables, I'm guessing your issue is that it prints the object types instead of the content, right? Something like this?

Code: Select all

PS /Users/vvvvvv> $someHashTable           
                                 
Name                           Value                   
----                           -----
Thursday                       2/24/22 12:00:00 AM
Wednesday                      2/23/22 12:00:00 AM
Tuesday                        2/22/22 12:00:00 AM
Monday                         2/21/22 12:00:00 AM

PS /Users/vvvvvv> $someHashTable | Export-CSV -NoTypeInformation -Path temp.csv
PS /Users/vvvvvv> Get-Content ./temp.csv
"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","4"
If so it's expected; hash-tables aren't text strings they're key value pairs and Export-CSV is looking for properties to match to columns and values: https://docs.microsoft.com/en-us/powers ... rshell-7.2 If you check your $someHashTable object, you'll see what properties Export-CSV is working on and why it dumps the object type instead of values beneath it.

Luckily, there's a cheap trick to fix this: Just type your table as a PSCustomObject by doing something like:

Code: Select all

$someCustomObject = [PSCustomObject]$someHashTable
However, I actually wouldn't use HashTables in this way, and you'd be better off making a Custom Object and a generic list instead. You were right to avoid normal Arrays (for too many objects it will get slow), but you're using hash tables to store what is better off as a property on an object.

Also, when you convert the Hash Table, it's going to output funny and you'll need to rebuild the list anyways to be more export friendly

Make a Generic list with the following:

Code: Select all

PS /Users/vvvvvv> using namespace System.Collections.Generic
PS /Users/vvvvvv> $someList = [List[Object]]@()
The first line is the same as using import in other languages like python and just saves typing later on, and then we make a Generic object list.

You can then parse your initial variable arrays and build an object from them to add to your list:

Code: Select all

PS /Users/vvvvvv> for($i=1;$i -lt 5; $i++){
>> $date = (Get-Date).AddDays($i)
>> $SomeObject = [PSCustomObject]@{
>> DayOfWeek = $Date.DayOfWeek
>> Date = $date.DateTime
>> }
>> $someList.Add($SomeObject)
>> }
PS /Users/vvvvvv> $someList

DayOfWeek Date
--------- ----
   Monday Monday, February 21, 2022 11:13:36 AM
  Tuesday Tuesday, February 22, 2022 11:13:36 AM
Wednesday Wednesday, February 23, 2022 11:13:36 AM
 Thursday Thursday, February 24, 2022 11:13:36 AM

PS /Users/vvvvvv> $someList | Export-CSV -NoTypeInformation -Path ListExport.csv
PS /Users/vvvvvv> Get-Content ./ListExport.csv
"DayOfWeek","Date"
"Monday","Monday, February 21, 2022 11:13:36 AM"
"Tuesday","Tuesday, February 22, 2022 11:13:36 AM"
"Wednesday","Wednesday, February 23, 2022 11:13:36 AM"
"Thursday","Thursday, February 24, 2022 11:13:36 AM"
PS /Users/vvvvvv> 
So in short, I'd change the following:

1. Don't use hash tables like this and instead just build initial variable arrays. You can actually add all of the VM objects together
2. Your logic get a bit muddled with the triple-nested for-each; I think there must be a cleaner way (I will try look later, but you might check this thread: powershell-f26/list-failed-servers-t15735.html it looks much cleaner)

Sorry for all the text and hope it helps.
David Domask | Product Management: Principal Analyst
alphonsodavies
Novice
Posts: 6
Liked: never
Joined: Feb 18, 2022 3:16 pm
Full Name: Alphone Davies
Contact:

Re: Export backup job status to .csv

Post by alphonsodavies »

Thank you David, i appreciate it. i'll try it out :)
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests