VMware Cloud Community
Keese
Contributor
Contributor
Jump to solution

Need helping pulling VM datastore information from multiple vCenters

Through Google magic and trial and error I've been able to develop the below script. The final metric I'd like to put the name of the datastore the VM is located on and I can't for the life of me figure out how to do it.

$myCol = @()

$TimeStamp = Get-Date -format yyyy-MM-dd

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$true | Out-Null

$vcenters = @(

    "server1",

    "server2",

    "server3",

    "server4"

);

Connect-VIServer -Server $vcenters -User username -Password password

ForEach ($Cluster in Get-Cluster)

    {

        ForEach ($vmhost in ($cluster | Get-VM))

        {

  $VMView = $VMhost | Get-View

                        $VMSummary = “” | Select Date, VMName, ClusterName, MemorySizeGB, LogicalCPUs, ProvisionedSpaceGB, ResourcePool, DataStore

                        $VMSummary.Date = $timestamp

                        $VMSummary.VMName = $VMhost.Name        

                        $VMSummary.ClusterName = $Cluster.Name

                        $VMSummary.MemorySizeGB = [Math]::Round($VMHost.memorymb / 1024, 2)

                        $VMSummary.LogicalCPUs = $VMHost.numcpu

                        $VMSummary.ResourcePool = $VMHost.ResourcePool

                        $VMSummary.ProvisionedSpaceGB = [Math]::Round($vmhost.ProvisionedSpaceGB, 2)

  $myCol +=$VMSummary

  }

            }

$myCol | sort VMName | Export-CSV C:\VM-$timestamp.csv

Any help would be appreciated.

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Looks like you might be using an older PowerCLI build.

Do a

Get-PowerCliVersion

What does it say ?

Consider upgrading to 5.5 R1


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Add the line

$VMSummary.Datastore = [string]::Join(',',(Get-Datastore -RelatedObject $VMHost | Select -ExpandProperty Name))


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

0 Kudos
Keese
Contributor
Contributor
Jump to solution

LucD, thanks for helping. I added the line and am receiving this output:

Get-Datastore : A parameter cannot be found that matches parameter name 'Relate

dObject'.

At C:\users\username\Desktop\PSscript.ps1:24 char:75

+             $VMSummary.Datastore = [string]::Join(',',(Get-Datastore -Related

Object <<<<  $VMHost | Select -ExpandProperty Name))

    + CategoryInfo          : InvalidArgument: (:) [Get-Datastore], ParameterB

   indingException

    + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCo

   re.Cmdlets.Commands.GetDatastore

Here is how the script looks now:

$myCol = @()

$TimeStamp = Get-Date -format yyyy-MM-dd

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$true | Out-Null

$vcenters = @(

    "server1",

    "server2",

    "server3",

    "server4"

);

Connect-VIServer -Server $vcenters -User username -Password password

ForEach ($Cluster in Get-Cluster)

    {

        ForEach ($vmhost in ($cluster | Get-VM))

        {

  $VMView = $VMhost | Get-View

                        $VMSummary = “” | Select Date, VMName, ClusterName, MemorySizeGB, LogicalCPUs, ProvisionedSpaceGB, ResourcePool, DataStore

                        $VMSummary.Date = $timestamp

                        $VMSummary.VMName = $VMhost.Name       

                        $VMSummary.ClusterName = $Cluster.Name

                        $VMSummary.MemorySizeGB = [Math]::Round($VMHost.memorymb / 1024, 2)

                        $VMSummary.LogicalCPUs = $VMHost.numcpu

                        $VMSummary.ResourcePool = $VMHost.ResourcePool

                        $VMSummary.ProvisionedSpaceGB = [Math]::Round($vmhost.ProvisionedSpaceGB, 2)

                        $VMSummary.Datastore = [string]::Join(',',(Get-Datastore -RelatedObject $VMHost | Select -ExpandProperty Name))

  $myCol +=$VMSummary

  }

            }

$myCol | sort VMName | Export-CSV C:\VM-$timestamp.csv

Thanks again.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you might be using an older PowerCLI build.

Do a

Get-PowerCliVersion

What does it say ?

Consider upgrading to 5.5 R1


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

0 Kudos
Keese
Contributor
Contributor
Jump to solution

Updating PowerCLI resolved the issue. Thanks, LucD.

0 Kudos