PowerShell script exchange
Post Reply
t481
Enthusiast
Posts: 82
Liked: 1 time
Joined: Apr 28, 2015 7:52 am
Contact:

Extract info from custom attribute

Post by t481 »

Hello all,
I've got my backup jobs to write to a custom attribute in Veeam called "Veeam Backups"

Does anyone know of a method by which I can extract the job name, backup time, backup server name and directory from this attribute so that they can be put into different columns in a csv file?

I'm using PowerCLI to extract data but I'm not sure how to achieve this easily. I tried using .Split() method but that caused an issue because of the multiple split options ":" and "[", "]" not to mention the fact that it then splits the time as well because of the formatting.

Any help would be appreciated.

Thanks in advance
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Extract info from custom attribute

Post by oleg.feoktistov »

Hi,

Sure, you can easily apply split by multiple delimiters over a customFields string and thus convert it to an array. Finally, just access array members:

Code: Select all

$resourcepool = Get-ResourcePool -Name <ResourcePoolName>
$delimiters = "[", "]", ","
$vms = Get-VM | where {$_.ResourcePool -eq $resourcepool}
foreach($vm in $vms) {
$annotation = Get-Annotation -Entity $vm | where {$_.Name -eq 'Veeam Backups'} | select Name, Value
if ($annotation.Value) {
$customValue = $annotation.Value -split {$delimiters -contains $_}
    $jobName = $customValue[1]
    $time = $customValue[4]
    $backupServer = $customValue[7]
    $backupFolder = $customValue.TrimStart()[12]    
}
}
Best regards,
Oleg
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Extract info from custom attribute

Post by oleg.feoktistov » 1 person likes this post

Honoring precision and attribute filtering, you can also take advantage of .NET regular expressions:

Code: Select all

$resourcepool = Get-ResourcePool -Name <ResourcePoolName>
$jobPattern = "Job name: \[\w*\W*\w*\W*\w*\]"
$timePattern = "Time: \[\d{1,2}\/\d{1,2}\/\d{1,4}\]"
$hostPattern = "Backup host: \[\w*\W*\w*\]"
$folderPattern = "Backup folder: \[\[\w*\W*\w*.\w*.\w*\] \w*\W*\w*\W*\w*\]"
$delimiters = "[", "]", ","
$vms = Get-VM | where {$_.ResourcePool -eq $resourcepool}
foreach($vm in $vms) {
$annotation = Get-Annotation -Entity $vm | where {$_.Name -eq 'Veeam Backups'} | select Name, Value
if ($annotation.Value) {

    $jobName = [regex]::Match($annotation.Value, $jobPattern).Value
    $jobName = $jobName -split {$delimiters -contains $_}
    $jobName[1]
    $time = [regex]::Match($annotation.Value, $timePattern).Value
    $time = $time -split {$delimiters -contains $_}
    $time[1]
    $backupHost = [regex]::Match($annotation.Value, $hostPattern).Value
    $backupHost = $backupHost -split {$delimiters -contains $_}
    $backupHost[1]
    
    $backupFolder = [regex]::Match($annotation.Value, $folderPattern).Value
    $backupFolder = $backupFolder -split {$delimiters -contains $_}
    $backupFolder.TrimStart()[3]
    }
}
Using this method, I just filtered attributes by precise patterns and displayed matching ones only.

Best regards,
Oleg
t481
Enthusiast
Posts: 82
Liked: 1 time
Joined: Apr 28, 2015 7:52 am
Contact:

Re: Extract info from custom attribute

Post by t481 »

Thank you very much indeed. That worked beautifully.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests