Using PowerCli to read out SRM values

Using PowerCli to read out SRM values

The script underneath will read out the SRM protection group and SRM Recovery Plan as well as other SRM information for each VM. You can then either export this as a csv or use them as tags in vCenter. The advantage is that you not only see those information directly in the VM summery but you also now can search for a SRM group and find all associated VMs.

#load VMware PowerCLi module

if ((Get-PSSnapin | where {$_.Name -ilike "VMware.VimAutomation.Core"}).Name -ine "VMware.VimAutomation.Core"){

    Write-Host "Loading VMware PowerCli"

    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

}

#define varibales

$ProtectedVM=@()

$RecoveryObj=@()

#basic connections

Write-Host "connecting vCenter"

#$VINCred=Get-Credential #interactive logon, renove comments from connect lines

$vcConnect=Connect-VIServer "myVC.mylab.local" #-Credential $VINCred

Write-Host "connecting SRM"

$srmConnect=Connect-SrmServer #-Credential $VINCred

try {

    $SRMApi = $srmConnect.ExtensionData

    #get all revovery plan and store

    Write-Host "Build Recovery Object List" -NoNewline

    foreach ($plan in $SRMApi.Recovery.ListPlans()){

        $tempObj= New-Object PSObject -Property @{"moref"=($plan.moref.Value);"Name"=($plan.GetInfo().Name)}

        $RecoveryObj += $tempObj

        Write-Host "." -NoNewline

    }

    Write-Host "`n"

   

    #get all protected VM (moref, potgroup and RecovPlan) and write to object

    Write-Host "Getting VM infos"

    foreach ($protGroup in $SRMApi.Protection.ListProtectionGroups()){

            #get ProtGroup name

            $protGroupName=$protGroup.GetInfo().Name

            Write-Host "`nWorking on Protection Group: "+$protGroupName -NoNewline

            #get PortGroup RecoveryPlan(s)

            $recoveryplan=$protGroup.ListRecoveryPlans()

            #A ProtectionGroup can belong to more then one RecoveryPlan

            $TempPlanName=""

            foreach($TempPlan in $recoveryplan){

                #find Recovery and get the name

                foreach ($test in $RecoveryObj) {

                    if ($TempPlan.moref.value -eq $test.moref){

                        $TempPlanName+=$test.Name

                    }

                }   

            }

            $recoveryPlanName=$TempPlanName -join ','

            #get all proetced VMs

            foreach ($protVM in $protGroup.ListProtectedVMs()){

                $tempObj= New-Object PSObject -Property @{"VMmoref"=($protVM.Vm.MoRef);"ProtGroup"=$protGroupName;"RecoPlan"=$recoveryPlanName;"State"=($protVM.State);"NeedConfig"=($protVM.NeedsConfiguration);"Faults"=($protVM.Faults)}

                $ProtectedVM+=$tempObj

                Write-Host "." -NoNewline

            }

    }#end of get VMs

    Write-Host "`n"

    #check if Tag Categories Exist, if not create

    if ((Get-TagCategory  -Name 'SRMPrortectionGroup' -ErrorAction:SilentlyContinue) -eq $null){

            Write-Host "Creating Tag Category SRMPortectionGroup"

            New-TagCategory "SRMPrortectionGroup" -Cardinality "Single" -EntityType VirtualMachine -Confirm:$false

    }

    if ((Get-TagCategory  -Name 'SRMRecoveryPlan' -ErrorAction:SilentlyContinue) -eq $null){

            Write-Host "Creating Tag Category SRMRecoveryPlan"

            New-TagCategory "SRMRecoveryPlan" -Cardinality "Single" -EntityType VirtualMachine -Confirm:$false

    }

    #loading TagCategories

    $TC_SRMGroup=Get-TagCategory  -Name 'SRMPrortectionGroup'

    $TC_SRMPlan=Get-TagCategory  -Name 'SRMRecoveryPlan'

   

    #Tagging Protected VMs

    Write-Host "Assigning tags" -NoNewline

    foreach ($vmObj in $ProtectedVM) {

        #get VMObject from Moref

        $VM=Get-VIObjectByVIView -MORef ($vmObj.VMmoref)

        #Check if Protection group tag exists

        if ((Get-Tag -Name ($vmObj.ProtGroup) -Category $TC_SRMGroup -ErrorAction:SilentlyContinue) -eq $null){

            Write-Host ("Creating Tag {0}" -f ($vmObj.ProtGroup))

            New-Tag -Name ($vmObj.ProtGroup) -Category $TC_SRMGroup -Confirm:$false

        }

        #assign Protection group tag

        $vm|New-TagAssignment -Tag (Get-Tag -Name ($vmObj.ProtGroup))  -Confirm:$false

       

        #Check if Protection group tag exists

        if ((Get-Tag -Name ($vmObj.RecoPlan) -Category $TC_SRMPlan -ErrorAction:SilentlyContinue) -eq $null){

            Write-Host ("Creating Tag {0}" -f ($vmObj.ProtGroup))

            New-Tag -Name ($vmObj.RecoPlan) -Category $TC_SRMPlan -Confirm:$false

        }

        #assign Recovery plan tag

        $vm|New-TagAssignment -Tag (Get-Tag -Name ($vmObj.RecoPlan)) -Confirm:$false

    }#end of Tagging

# Write-Host "Exporting VM info"   

# $ProtectedVM|select-Object VMmoref,ProtGroup,RecoPlan,State,NeedConfig | Sort-Object -Property VMmoref|Export-Csv -Path d:\tmp\Protvms.csv

} #end of try

Catch {

    Write-Host $_.Exception.Message -ForegroundColor Red

}

Finally{

    Write-Host "Disconnecting vCenter"

    Disconnect-SrmServer $srmConnect -Confirm:$false -Force:$true

    Write-Host "Disconnecting SRM"

    Disconnect-VIServer $vcConnect -Confirm:$false -Force:$true

}

Version history
Revision #:
1 of 1
Last update:
‎09-17-2015 01:59 AM
Updated by: