Try this, it's worked for me # Clear initial variables $vCenters = $null $vc = $null $logfolder = $null $outputfile = $null $srmDailyReport = $null # set location of output data $lo...
See more...
Try this, it's worked for me # Clear initial variables $vCenters = $null $vc = $null $logfolder = $null $outputfile = $null $srmDailyReport = $null # set location of output data $logfolder = "C:\Temp\SRM_PGs" if (!(Test-Path -Path $logFolder)) { New-Item -ItemType Directory -Path $logFolder } Set-Location -Path $logFolder $outputfile = (Get-Date -Format yyyy-MMM-dd-HHmm) + ".csv" $srmDailyReport=@() # Create array with vCenter Names, add/change vCenters as necessary [string[]] $vCenters = "vcsa1.comp.com", "vcsa2.comp.com" foreach ($vc in $vCenters) { # Clear variables $srmConnection = $null $srmApi = $null $protectionGroupInfo = $null $protectionGroups = $null $protectionGroup = $null $protectedVms = $null $associatedVms = $null $vm = $null $VmNeedsSrmConfig = $null $RecoveryPlan = $null $view = $null Connect-VIServer $vc -username "comp\USERNAME" -password "notThisPassword!" Connect-SrmServer -User "comp\USERNAME" -Password "notThisPassword!" -OutVariable srmConnection $srmIP = $srmConnection.Name $srmServerName = ([System.Net.dns]::GetHostEntry($srmIP)).HostName $srmApi = $srmConnection.ExtensionData $protectionGroups = $srmApi.Protection.ListProtectionGroups() # Begin TEST BLOCK # To run script on an individual Protection Group, uncomment two lines below (comment out line 72 if testing) #$TestProtectionGroup = $protectionGroups | where {$_.GetInfo().Name -eq "Archer" } #$TestProtectionGroup | where {$_.GetProtectionState() -ne "Shadowing" } | % { # End TEST BLOCK # If a Protection Group has a status of Shadowing, no data exists within this local SRM Server. Connect to alternate SRM Server and proceed # If a Protection Group has a status of Recovered or Ready, proceed to collect local data # Comment out the below line when testing using individual Protection Groups $protectionGroups | where {$_.GetProtectionState() -ne "Shadowing" } | % { $protectionGroup = $_ $protectionGroupInfo = $protectionGroup.GetInfo() $RecoveryPlan = $protectionGroup.ListRecoveryPlans() # If a Protection Group has a status of Recovered, update available fields if ($protectionGroup.GetProtectionState() -eq "Recovered") { $GeneralProp = [ordered]@{ 'Production Cluster'="" 'Protection Group' = $protectionGroupInfo.Name 'Recovery Plan Name' = $RecoveryPlan.GetInfo().Name 'Needs SRM Config' = 'Reprotect Needed' } $srmDailyReport += New-Object -TypeName psobject -Property $GeneralProp } # If a Protection Group has a status of Ready, update available fields else{ # To get all VM's in the Protected Datastore, this should be all VM's associated with Protection Group $associatedVms = $protectionGroup.ListProtectedDatastores() | Get-VIObjectByVIView | Get-VM # The following lists the virtual machines protected by 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() } foreach ($vm in $associatedVms) { $GeneralProp = [ordered]@{} $GeneralProp.Add('Production Cluster',$vm.VMHost.Parent.Name) $GeneralProp.Add('Protection Group',$protectionGroupInfo.Name) $GeneralProp.Add('Recovery Plan Name' , $RecoveryPlan.GetInfo().Name) $VmNeedsSrmConfig = $protectedVms | where {$protectedVms.vm.name -eq $vm.Name} if ($VmNeedsSrmConfig.needsconfiguration){ $GeneralProp.Add('Needs SRM Config','') } else{ $GeneralProp.Add('Needs SRM Config','VM Needs Config') } $GeneralProp.Add('Vm Name' , $vm.name) $GeneralProp.Add('Guest O/S' , $vm.GuestId) $view = $vm | Get-View $vmxfile = $view.Config.Files.VmPathName $vmxLocation = $vmxfile.split(" ")[0].TrimStart("[").TrimEnd("]") $GeneralProp.Add(".vmx Location", $vmxLocation) $GeneralProp.Add("Tools Recommendation", $view.Guest.ToolsVersionStatus) $srmDailyReport += New-Object -TypeName psobject -Property $GeneralProp } # close "foreach ($vm in $associatedVms)" } # close else } # close "$protectionGroups | where {$_.GetProtectionState() -ne "Shadowing" }" Disconnect-VIServer -Server $vc -Confirm:$false -Force } # close "foreach ($vc in $vCenters)" # Create the .csv file $srmDailyReport | Sort-Object -Property 'Protection Group', 'Vm Name' | Export-Csv $outputfile -NoTypeInformation Invoke-Item $outputfile