Working on a script to show SRM protection status of VMs.
I adapted the basic code from here to add more detail.
https://vdc-download.vmware.com/vmwb-repository/dcr-public/847b8d79-9752-43d3-8217-8270ab647679/1e99...
Here's my code. All seems to be working except if there is a fault, the Faults column shows {VMware.Vim.LocalizedMethodFault} instead of what the fault is. I can't figure out how to expand this to show fault details.
$VMWCreds = Get-Credential -Message 'Please provide credentials'
Connect-VIServer -Server 'VC.domain.com' -Protocol 'https' -Credential $VMWCreds
$srmConnection = Connect-SrmServer -SrmServerAddress 'SRM.domain.com' -Protocol 'https' -Credential $VMWCreds
$srmApi = $srmConnection.ExtensionData
$protectionGroups = $srmApi.Protection.ListProtectionGroups()
[System.Collections.ArrayList] $VMProtectionDataAll = @()
$protectionGroups | % {
$protectionGroup = $_
$protectionGroupInfo = $protectionGroup.GetInfo()
# The following command lists the virtual machines associated with a protection group
$protectedVms = $protectionGroup.ListProtectedVms()
# The result of the above call is an array of references to the virtual machines at the vSphere API
# To populate the data from the vSphere connection, call the UpdateViewData method on each virtual machine view object
$protectedVms | % { $_.Vm.UpdateViewData() }
# After the data is populated, use it to generate a report
ForEach ( $tmpVM in $protectedVms ) {
[void] $VMProtectionDataAll.Add([PSCustomObject][Ordered] @{
'VMName' = $tmpVM.VmName
'ProtectionGroup' = $protectionGroupInfo.Name
'State' = $tmpVM.State
'PeerState' = $tmpVM.PeerState
'NeedsConfiguration' = $tmpVM.NeedsConfiguration
'Faults' = $tmpVM.Faults
})
}
}
$VMProtectionDataAll | Format-Table -A
Here's a hackish variation that shows the contents of $error.exception. In my example, the code below shows the error as:
"The object 'vim.VirtualMachine:vm-363' has already been deleted or has not been completely created"
The SRM mgmt page shows protection status as:
"Invalid: Virtual machine 'SERVER01' is no longer protected. VM 'SERVER01' is not replicated by VR. Protected VM deleted."
I'd like to capture that detail message if possible.
$VMWCreds = Get-Credential -Message 'Please provide credentials'
Connect-VIServer -Server 'VC.domain.com' -Protocol 'https' -Credential $VMWCreds
$srmConnection = Connect-SrmServer -SrmServerAddress 'SRM.domain.com' -Protocol 'https' -Credential $VMWCreds
$srmApi = $srmConnection.ExtensionData
$protectionGroups = $srmApi.Protection.ListProtectionGroups()
[System.Collections.ArrayList] $VMProtectionDataAll = @()
$protectionGroups | % {
$protectionGroup = $_
$protectionGroupInfo = $protectionGroup.GetInfo()
# The following command lists the virtual machines associated with a protection group
$protectedVms = $protectionGroup.ListProtectedVms()
# The result of the above call is an array of references to the virtual machines at the vSphere API
# To populate the data from the vSphere connection, call the UpdateViewData method on each virtual machine view object
#$protectedVms | % { $_.Vm.UpdateViewData() }
# After the data is populated, use it to generate a report
ForEach ( $tmpVM in $protectedVms ) {
$Error.Clear()
$tmpVM.Vm.UpdateViewData()
If ( $null -ne $Error.Exception ) {
$tmpErrorMsg = ($Error.Exception.Message ).Replace("Exception calling `"UpdateViewData`" with `"0`" argument(s):",'')
} Else {
$tmpErrorMsg = $null
}
[void] $VMProtectionDataAll.Add([PSCustomObject][Ordered] @{
'VMName' = $tmpVM.VmName
'ProtectionGroup' = $protectionGroupInfo.Name
'State' = $tmpVM.State
'PeerState' = $tmpVM.PeerState
'NeedsConfiguration' = $tmpVM.NeedsConfiguration
'Faults' = $tmpVM.Faults
'ErrorDetail' = $tmpErrorMsg
})
}
}
$VMProtectionDataAll | Format-Table -A
For your Faults, line, try the following:
'Faults' = If ($tmpVM.Faults){$tmpVM.Faults.localizedMessage} Else {$null}
I haven't been able to test this myself (couldn't find faults to test against in SRM), but referencing the properties for vim.vim.LocalizedMethod.Fault led me to the dataobject reference as having a property localizedMessage - type string.