VMware Cloud Community
jayce752
Contributor
Contributor

Export SRM Report to CSV File

This is probably really simple to do but I'm not having any luck - I'm running the following script to create a report of VMs and their Protection Groups from SRM. The script runs successfully and displays the data in the powercli window, however I need this to be exported to a CSV file.

I've attempted using the Export-CSV command with the NoTypeInformation flag and while it does generate the csv file, it only returns one column of GUIDs instead of the list of VMs and their Protection Groups like the results in the PowerCLI window. Can anyone give me any tips to get this to export to csv with the actual data instead of GUID? Thanks!

 

$srmApi = $srmConnection.ExtensionData

$protectionGroups = $srmApi.Protection.ListProtectionGroups()

$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} }

0 Kudos
1 Reply
LucD
Leadership
Leadership

The issue is due to the fact that a ForEach (alias %) is not placing anything on the pipeline.

You can bypass that capturing the out in a variable, and then piping that variable to Export-Csv

$srmApi = $srmConnection.ExtensionData

$protectionGroups = $srmApi.Protection.ListProtectionGroups()


$report = $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

   }

}


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


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

0 Kudos