Obtain total used storage capacity with PowerCLI

One of my recent tasks involved taking a SAN out of action for maintenance and I needed to ensure the remaining SANs had enough capacity to run all of our VMs. I could have simply looked at the used space in the vSphere GUI, but this includes powered off VMs which I wasn’t interested in.

The following PowerCLI function totals the used capacity for powered on VMs.