Last active
February 16, 2018 08:37
-
-
Save vMarkusK/49865e5c7b56ea0fb96a4dfe4994c9c5 to your computer and use it in GitHub Desktop.
Push Custom Properties about Veeam Protection into VMware vRealize Operations Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Credits: http://vman.ch/vrops-suite-api-properties-import/ | |
#region: SSL Config for SelfSigned Cert and force TLS 1.2 | |
add-type @" | |
using System.Net; | |
using System.Security.Cryptography.X509Certificates; | |
public class TrustAllCertsPolicy : ICertificatePolicy { | |
public bool CheckValidationResult( | |
ServicePoint srvPoint, X509Certificate certificate, | |
WebRequest request, int certificateProblem) { | |
return true; | |
} | |
} | |
"@ | |
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
#endregion | |
#region: Functions | |
function Get-VeeamProtectionEnhanced { | |
<# | |
.NOTES | |
Forked From: | |
https://github.com/mycloudrevolution/Veeam-vSphere-Interactions/blob/master/Veeam.PowerCLI-Interactions/functions/Get-VeeamProtection.psm1 | |
#> | |
#Requires -Version 4 | |
#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"} | |
#Requires -PSSnapin VeeamPssnapin | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] | |
[ValidateNotNullorEmpty()] | |
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl[]] | |
$myVMs | |
) | |
Begin { | |
if ($global:DefaultVIServer) { | |
Write-Verbose "VI Server Connected" | |
if ($ViServer = Get-VBRServer -Name $global:DefaultVIServer.Name) { | |
Write-Verbose "VI Server '$($ViServer.Name)' in Veeam Inventory found" | |
} | |
else { | |
throw "VI Server not in Veeam found!" | |
} | |
} | |
else { | |
throw "no VI Server Connected!" | |
} | |
$VbrReastorePoints = Get-VBRRestorePoint | |
$VBRServerObject = Get-VBRServer | Where-Object {$_.Description -eq "Backup server" -and $_.Type -eq "Local"} | |
} | |
Process { | |
$MyView = @() | |
foreach ($MyVm in $MyVms) { | |
[DateTime] $NowDate = (Get-date) | |
[int64] $NowDateEpoc = Get-Date -Date $NowDate.ToUniversalTime() -UFormat %s | |
$NowDateEpoc = $NowDateEpoc*1000 | |
$MoRef = $MyVm.ExtensionData.MoRef.Value | |
if ($VeeamVm = Find-VBRViEntity -Server $ViServer | Where-Object {$_.Reference -eq $MoRef}) { | |
[Array]$VmRestorePoints = $VbrReastorePoints | Where-Object {$_.InsideDir.Split("(,)") -eq $MoRef} | |
if ($VmRestorePoints.count -gt 0) {$IsProtected = $true} else {$IsProtected = $false} | |
$LastRestorePoint = $VmRestorePoints | Sort-Object CreationTime | Select-Object -Last 1 | |
$LastBackupJob = Get-VBRBackup | Where-Object {$_.Id -eq $LastRestorePoint.BackupId} | |
$FirstRestorePoint = $VmRestorePoints | Sort-Object CreationTime | Select-Object -First 1 | |
$Report = [PSCustomObject] @{ | |
VmName = $VeeamVm.name | |
MoRef = $MoRef | |
IsProtected = $IsProtected | |
VmRestorePoints = $VmRestorePoints.count | |
LastRestorePoint = $LastRestorePoint.CreationTime | |
LastBackupJob = $LastBackupJob.Name | |
FirstRestorePoint = $FirstRestorePoint.CreationTime | |
BackupServer = $VBRServerObject.Name | |
VmObject = $MyVm | |
Date = $NowDate | |
DateEpoch = $NowDateEpoc | |
} | |
$MyView += $Report | |
} | |
} | |
$MyView | |
} | |
} | |
#endregion | |
#region: Pre-Requirments | |
Import-Module VMware.VimAutomation.Core, VMware.VimAutomation.vROps | |
Add-PSSnapin VeeamPssnapin | |
$WarningPreference = "SilentlyContinue" | |
#endregion | |
#region: Global definitions | |
$vRopsFQDN = <Your FQDN> | |
$vCenterFQDN = <Your FQDN> | |
$VeeamFQDN = <Your FQDN> | |
$vRopsCred = Import-Clixml -Path "C:\Secure\vRops-Credential.xml" | |
$vCenterCred = Import-Clixml -Path "C:\Secure\vCenter-Credential.xml" | |
$VeeamCred = Import-Clixml -Path "C:\Secure\Veeam-Credential.xml" | |
$vCenterDcFilter = "Clustered" | |
#endregion | |
#region: Connections | |
Connect-VIServer -Server $vCenterFQDN -Credential $vCenterCred | |
Connect-OMServer -Server $vRopsFQDN -Credential $vRopsCred | |
Connect-VBRServer -Server $VeeamFQDN -Credential $VeeamCred | |
#endregion | |
#region: Get Protected VMs | |
$ProtectedVms = Get-Datacenter -Name $vCenterDcFilter | Get-VM | Get-VeeamProtectionEnhanced | |
if (!$ProtectedVms) { | |
Throw "No Veeam Protected VMs found" | |
} | |
#endregion | |
#region: Create XML Structure and populate variables from the Metadata file | |
foreach ($ProtectedVm in $ProtectedVms) { | |
$XmlFile = @('<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<ops:property-contents xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<ops:property-content statKey="VEEAM|LastRestorePoint"> | |
<ops:timestamps>{0}</ops:timestamps> | |
<ops:values>{1}</ops:values> | |
</ops:property-content> | |
<ops:property-content statKey="VEEAM|LastBackupJob"> | |
<ops:timestamps>{0}</ops:timestamps> | |
<ops:values>{2}</ops:values> | |
</ops:property-content> | |
<ops:property-content statKey="VEEAM|FirstRestorePoint"> | |
<ops:timestamps>{0}</ops:timestamps> | |
<ops:values>{3}</ops:values> | |
</ops:property-content> | |
<ops:property-content statKey="VEEAM|IsProtected"> | |
<ops:timestamps>{0}</ops:timestamps> | |
<ops:values>{4}</ops:values> | |
</ops:property-content> | |
<ops:property-content statKey="VEEAM|VmRestorePoints"> | |
<ops:timestamps>{0}</ops:timestamps> | |
<ops:data>{5}</ops:data> | |
</ops:property-content> | |
<ops:property-content statKey="VEEAM|BackupServer"> | |
<ops:timestamps>{0}</ops:timestamps> | |
<ops:values>{6}</ops:values> | |
</ops:property-content> | |
</ops:property-contents>' -f $ProtectedVm.DateEpoch, | |
$ProtectedVm.LastRestorePoint, | |
$ProtectedVm.LastBackupJob, | |
$ProtectedVm.FirstRestorePoint, | |
$ProtectedVm.IsProtected, | |
$ProtectedVm.VmRestorePoints, | |
$ProtectedVm.BackupServer | |
) | |
[xml]$XmlSend = $XmlFile | |
$OmRessource = Get-OMResource -Entity $ProtectedVm.VmObject | |
if ($OmRessource) { | |
$UrlSend = 'https://' + $vRopsFQDN + '/suite-api/api/resources/'+ $OmRessource.Id + '/properties' | |
$ContentType = "application/xml;charset=utf-8" | |
Invoke-RestMethod -Method POST -uri $UrlSend -Body $XmlSend -Credential $vRopsCred -ContentType $ContentType -Verbose | |
} | |
else { | |
Throw "No vRealize Operations Manager Ressource for '$($ProtectedVm.VmName)' Found" | |
} | |
} | |
#endregion | |
#region: Disconnect | |
Disconnect-VIServer -Server * -Force:$true -Confirm:$false | |
Disconnect-OMServer -Server * -Force:$true -Confirm:$false | |
Disconnect-VBRServer | |
#endregion | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment