Automation

 View Only
  • 1.  SRM status and fault detail

    Posted Sep 14, 2022 10:12 PM

    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/1e992a3d-ac47-4453-8182-457145215c40/GUID-95273F92-CAE5-45F4-9DE9-E8C20B022185.html

    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

     

     



  • 2.  RE: SRM status and fault detail

    Posted Sep 14, 2022 10:45 PM

    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

     

     



  • 3.  RE: SRM status and fault detail

    Posted Sep 22, 2022 04:25 PM

    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.