VMware Cloud Community
Baoth
Enthusiast
Enthusiast
Jump to solution

PowerCLI - SRM reporting - You cannot call a method on a null-valued expression

Hello all

Hoping someone might be able to help me please.

According to the Documentation Centre here, you can create a report of the Protected VM's using the below script:

1. List all protection groups associated with the SRM server.

$srmApi = $srm.ExtensionData

$protectionGroups = $srmApi.Protection.ListProtectionGroups()

2. Generate a report of the protected virtual machines.

$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

    $protectedVms | %{

        $output = "" | select VmName, PgName

        $output.VmName = $_.Vm.Name

        $output.PgName = $protectionGroupInfo.Name

        $output

    }

} | Format-Table @{Label="VM Name"; Expression={$_.VmName} }, @{Label="Protection group name"; Expression={$_.PgName} }

The problem I am having is in step 1, specifically this part: $protectionGroups = $srmApi.Protection.ListProtectionGroups()

I'm using PowerCLI version VMware-PowerCLI-6.3.0-3639347, and can successfully connect-viserver and subsequently connect-srmserver works too.

The first variable of $srmApi = $srm.ExtensionData is set successfully, but the second returns the error "You cannot call a method on a null-valued expression".

Here's the exact output from the CLI session:

PowerCLI C:\> $protectionGroups = $srmApi.Protection.ListProtectionGroups()

You cannot call a method on a null-valued expression.

At line:1 char:1

+ $protectionGroups = $srmApi.Protection.ListProtectionGroups()

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

PowerCLI C:\>

My end goal is to have a script that will create a list of all SRM protected VM's within the estate, but I would also like to have additional information for the VM's - like vCPU count, vRAM, hostname and OS version for example. I just can't even get over the first hurdle.

Thanks all.

50 Replies
LucD
Leadership
Leadership
Jump to solution

Have you looked at the Get-SrmConfigReportProtectedVm function in ReportConfiguration.ps1?

Does the State property give you that?

Get-SrmProtectionGroup |

   ForEach-Object -Process {

   $pg = $_

   $pgInfo = $pg.GetInfo()

   Get-SrmProtectedVM -ProtectionGroup $pg |

   ForEach-Object -Process {

   $pVm = $_

   Get-VM -Name  $pVm.Vm.Name |

  Select @{N = "VM Name"; E = {$_.Name} },

   @{N = 'State'; E = {$pVm.State}},

  NumCpu, MemeoryGB,

   @{N = "Protection group name"; E = {$pgInfo.Name}}

   }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Baoth
Enthusiast
Enthusiast
Jump to solution

It does output information I would expect to see, but it still loops through the same VM's over and over.

So the first PG its reporting on, happens to have 14 VM's within it. The scripts (apart from the original one from the GitHub page) are reporting the same 14 VM's per Protection Group, but changing the PG name on the output.

Happy to try more with your help, but I think its a nice to have that the architect I am working with can go without in reality.

Thanks to all the help, I can now get a list of VM's that are protected, and what protection group they are in. From that output, I can create a new text file that contains the VM's and run "get-vm $vmList | Select-object Name, Guest, UsedSpaceGB, ProvisionedSpaceGB, MemoryGB, NumCPU, Notes | Export-Csv <path>.csv" which contains the information the architect wants.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was a typo in the previous script, I used $pg instead of $pgInfo.
I corrected that, can you try again?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Baoth
Enthusiast
Enthusiast
Jump to solution

No problem. All red output on that one:

Get-SrmProtectedVM : Cannot process argument transformation on parameter 'ProtectionGroup'. Cannot convert the "VMware.VimAutomation.Srm.Views.SrmProtectionGroupInfo" value of type

"VMware.VimAutomation.Srm.Views.SrmProtectionGroupInfo" to type "VMware.VimAutomation.Srm.Views.SrmProtectionGroup[]".

At line:6 char:40

+    Get-SrmProtectedVM -ProtectionGroup $pgInfo |

+                                        ~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-SrmProtectedVM], ParameterBindingArgumentTransformationException

    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-SrmProtectedVM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And what with the last change I did above?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Baoth
Enthusiast
Enthusiast
Jump to solution

Looks good! I added the additional info I needed too:

Get-SrmProtectionGroup |

   ForEach-Object -Process {

   $pg = $_

   $pgInfo = $pg.GetInfo()

   Get-SrmProtectedVM -ProtectionGroup $pg |

   ForEach-Object -Process {

   $pVm = $_

   Get-VM -Name  $pVm.Vm.Name |

  Select @{N = "VM Name"; E = {$_.Name} },

   @{N = 'State'; E = {$pVm.State}},

  NumCpu, MemoryGB, Guest, UsedSpaceGB, ProvisionedSpaceGB, Notes,

   @{N = "Protection group name"; E = {$pgInfo.Name}}

   }

} | Export-Csv -Path <path-here> -NoTypeInformation -UseCulture

The State column is SRM Protection state, isn't it?

As an aside, there are some errors which generate the following text. Not sure why, but I've got another output from another trial which contains the moref of a VM that apparently has no name, but that tells me what PG it belongs to. I've compared the GUI to that, identified the VM in question, and get-vm returns its name without any problem.

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At line:14 char:18

+    Get-VM -Name  $pVm.Vm.Name |

+                  ~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could those be VMs that were added to a protection group but have been removed/replaced since?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Baoth
Enthusiast
Enthusiast
Jump to solution

Looks like VM's that are not being replicated by VR.

Not the end of the world to be fair, as there shouldn't be any issues within SRM PG's anyway. Once resolved, it should appear in the report. I'll have a look into it and confirm back.

Reply
0 Kudos
Baoth
Enthusiast
Enthusiast
Jump to solution

Resolved the replication issue on the one VM, and re-ran the script. As before, data was populated in the following format:

VM Name                 VM MoRef                  Needs Config VM Protection State Protection Group            Recovery Plans Recovery           Priority

                                                                                                                             

-------                 --------                  ------------ ------------------- ----------------            -------------- --

There was a bunch of red text in the Powershell window though.

Four of the below popped up:

Get-SrmProtectedVM : Exception calling "UpdateViewData" with "0" argument(s): "The object has already been deleted or has not

been completely created"

At line:12 char:17

+         $pvms = Get-SrmProtectedVM -ProtectionGroup $pg

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-SrmProtectedVM

There were more of these errors - roughly one error for every 2 VM's:

Exception calling "Join" with "2" argument(s): "Value cannot be null.

Parameter name: values"

At line:26 char:13

+             $output.plans = [string]::Join(",`r`n", $rpnames)

+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ArgumentNullException

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The 2nd error would mean that $rpnames is empty.

Avoid the error with

$output.plans = [string]::join(",`r`n", @($rpnames))


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Baoth
Enthusiast
Enthusiast
Jump to solution

Thanks. Couldn't get it to affect the output, but in fairness I seemed to be getting different errors.

Thanks for all the help with this. The issues within the title have been resolved, and I have a good report out of the system.

For anyone that comes across this in the future, the answers are throughout the thread rather than just the one I have marked. Credit where its due, so thanks to LucD​​​ and vXav​​ for their help.

For a quick reference:

Incompatibility with PowerCLI and the versions of other products

Using "vmware.vimautomation.srm -MaximumVersion 10.0.0.7893900" avoids a known bug in version PowerCLI version 11

These two points were fundamental in getting the SRM connection working. After that, the scripting and Git functions started to fall together nicely.

Reply
0 Kudos