This post is about using PowerCLI to access SRM Server. Specifically, we'll be diving into the topic of returning protected virtual machines. I will show the basics of getting connected and show off a new function I wrote.
This article is focused on vSphere 6.0/6.5 environments. However, your PowerCLI should be latest such as PowerCLI 6.5.4 (desired, not required).
You don't have to be some super SRM administrator; Just be experienced with PowerCLI, and you are good to go. Of course, the vCenters you connect to must be part of SRM.
You probably already have access to the SRM Servers, even if you don't know their names (ah wait, I'll do a get-vm and...). No need, VC already knows its SRM server, and we'll go into that too.
Let's kick it off with a new function I wrote called Get-ProtectedVMList which you can check out on github.
It's a PowerShell script that talks to vCenter and SRM to return a list of protected virtual machines (either as MoRef Ids or virtual machine objects). I've been using this quite a bit lately so I figured I would share it.
It's based on some great prior art from Cody Hosterman, entitled (findsrmVMs.ps1). Looking at his script is how I learned the
Connect-SrmServer cmdlet (thanks man!). It's quite easy once you get the hang of it. If you're late to the game on this like me, then jump right in.
Get the Script
Just download my
Get-ProtectedVMList function and dot source it. Here I show a couple of techniques to get started. Personally, I like
Import-Module, even against simple functions like this
. c:\temp\Get-ProtectedVMList.ps1 -or- Import-Module c:\temp\Get-ProtectedVMList.ps1
Once you run one of the above commands, the function will become available. A quick way to check is to run the help:
Connecting to VC and SRM
This is required before actually running my script. Basically, you just...
Connect-SrmServer is not using any parameters. Also, we don't need to know the SRM Servers (VC already knows this). Please note that you should have a
Multiple (at least for your session scope).
Tip:If you have problems with SSPI (even though your login is good) then try saving your Credential to a variable (i.e.
$creds = Get-Credential), then use that to login to VC, SRM or both.
I encourage you to review the output of
$Global:DefaultSrmServers to see the detail of your SRM session(s).
Then, if you dig into my script, you will see how we take advantage of this global variable to access the SRM API.
The following is an example with generalized info. Your environment will, of course, have unique values.
PS C:\> Connect-SrmServer ServiceUri : https://<ip-srm-server-a>:9086/vcdr/extapi/sdk SessionSecret : "QcRGEkifgxTjeYuZdzSJtKm1d6340f1SHrJagcGN" User : firstname.lastname@example.org IsConnected : True Port : 9086 Version : 6.1.1 Build : 4535903 ProductLine : srm InstanceUuid : 47ea774b-71c6-4a23-8440-7ac6a4382eb0 RefCount : 1 ExtensionData : VMware.VimAutomation.Srm.Views.SrmServiceInstance Uid : /SrmServer=someuser@<ip-srm-server-a>:9086/ Id : /SrmServer=someuser@<ip-srm-server-a>:9086/ Name : <ip-srm-server-a> IsInUse : True
Note: You would also see output for your other connected SRM server (not shown).
Ok. That's enough reviewing the basics. Let's get a list of protected virtual machines by using my
Results / Output
Now that we're connected, we can just run
Get-ProtectedVMList with no parameters, which returns MoRef Ids along with SRM info for the protected VMs. However, if using the
PassThru parameter, the virtual machine objects are returned instead (just like in a normal
$report = Get-ProtectedVMList
$ObjReport = Get-ProtectedVMList -PassThru
When you run these, save them to a variable. Then you can index into the array of objects or send to CSV, perform counts, etc.
Ok, so that's not too bad. We learned how to leverage vCenter to grab some details about the SRM server it participates with, and then do some stuff with the output. We can choose between staying lightweight with MoRefs (which includes SRM information), or just grab the virtual machine objects.
APPENDIX / EXAMPLES
In this appendix section, I'll dig a little deeper into some topics such as connecting, and problems you may face. Also, I show some example output.
This example connects to the desired vCenter Servers using SSPI and then runs the native Connect-SrmServer cmdlet (with no parameters). Next, we run the script and save it to a variable. We finish by disconnecting the runtime PowerCLI sessions to the SRM and vCenter Servers.
Connect-VIServer vcenter01,vcenter02 Connect-SrmServer #with no params $report = Get-ProtectedVMList Disconnect-SrmServer * -Confirm:$false Disconnect-VIServer * -Confirm:$false
This example saves a credential to a variable. Use this technique if having problems with SSPI.
$creds = Get-Credential Connect-VIServer vcenter01,vcenter02 -Credential $creds Connect-SrmServer -Credential $creds $report = Get-ProtectedVMList Disconnect-SrmServer * -Confirm:$false Disconnect-VIServer * -Confirm:$false
This example shows the full detail for the first protected virtual machine returned.
$report = Get-ProtectedVMList $report MoRef : VirtualMachine-vm-119224 ProtectedVm : protected-vm-26246 PeerProtectedVm : protected-vm-9542 State : Ready PeerState : Shadowing NeedsConfiguration : False ProtectionGroup : GoldProtectionGroup
This example shows how to get the virtual machine objects from one or more MoRefs (i.e., when not using the
Here we get all objects in a single call to Get-VM, which consumes an array of MoRef Ids. Then we show the first virtual machine in the returned report. Consider using the
PassThru switch instead.
$report = Get-ProtectedVMList $objReport = Get-VM -Id $report.MoRef $objReport Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- SuperVM001 PoweredOn 4 16.000
This example illustrates that PassThru returns a single virtual machine object for each protected virtual machine.
In the below,
$report will contain all virtual machines participating in SRM (including the
PeerProtected virtual machines in your secondary site).
$objReport in the example will only have the actual powered on primary virtual machines.
$report = Get-ProtectedVMList $objReport = Get-ProtectedVMList -PassThru $report.Count 88 $ObjReport.Count 44
Next and Todo
It will be interesting to gather results before and after bubble testing. Also, I'm sure some property results would change in differing scenarios.
There is a ton more in the API if you browse around. Thanks and have fun!