Enthusiast
Enthusiast

get the number of vms for each esx

Jump to solution

Hi,

I carried out this script to have the list of vms for each esx, I would also have to display the number of vms by esx too,

but a problem appears during the execution,

no problem without line 17, but when I insert line 17 I have an error

Can you help, please

thank you

Get-Module -Name VMware* -ListAvailable | Import-Module

$user = 'root'

$pswd = Read-Host -Prompt "Please enter your Password:"

$report = @()

Import-Csv -Path C:\Scripts\Tags_patch\ressources\List_test.csv -UseCulture | %{

    $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

    $report += (Get-VM -Server $srv |

   

    Select @{N='VMHost';E={$_.VMHost.Name}},

        @{N=“NumVM“;E={($_ | Get-VM).Count}},

        Name,PowerState,

        @{n="HardDiskSizeGB"; E={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}},

        @{N='GuestOS';E={$_.Guest.OSFullName}},

        @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}})

  

    Disconnect-VIServer -Server $srv -Confirm:$false

}

$report | Export-Csv -Path C:\Scripts\Tags_patch\resultats\test.csv -NoTypeInformation -UseCulture

0 Kudos
1 Solution

Accepted Solutions
Leadership
Leadership

The pipeline variable does contain a single VM where you use it.

Try like this

$user = 'root'

$pswd = Read-Host -Prompt "Please enter your Password:"


$report = @()

Import-Csv -Path C:\Scripts\Tags_patch\ressources\List_test.csv -UseCulture | %{

    $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

    $vms = Get-VM -Server $srv

    $report += $vms | Select @{N='VMHost';E={$_.VMHost.Name}},

        @{N=“NumVM“;E={$vms.Count}},

        Name,PowerState,

        @{n="HardDiskSizeGB"; E={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}},

        @{N='GuestOS';E={$_.Guest.OSFullName}},

        @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}}

    Disconnect-VIServer -Server $srv -Confirm:$false

}


$report | Export-Csv -Path C:\Scripts\Tags_patch\resultats\test.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
6 Replies
Leadership
Leadership

It would help if you show the error.


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

0 Kudos
Enthusiast
Enthusiast

Sorry

i made a mistake,

I have no error but the result file returns me a value of 0 in the number of vms

for example  i have 2 esx, and the result is like the attached file

0 Kudos
Leadership
Leadership

The pipeline variable does contain a single VM where you use it.

Try like this

$user = 'root'

$pswd = Read-Host -Prompt "Please enter your Password:"


$report = @()

Import-Csv -Path C:\Scripts\Tags_patch\ressources\List_test.csv -UseCulture | %{

    $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

    $vms = Get-VM -Server $srv

    $report += $vms | Select @{N='VMHost';E={$_.VMHost.Name}},

        @{N=“NumVM“;E={$vms.Count}},

        Name,PowerState,

        @{n="HardDiskSizeGB"; E={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}},

        @{N='GuestOS';E={$_.Guest.OSFullName}},

        @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}}

    Disconnect-VIServer -Server $srv -Confirm:$false

}


$report | Export-Csv -Path C:\Scripts\Tags_patch\resultats\test.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
Enthusiast
Enthusiast

i have an error on result :

C:\Scripts\Tags_patch\programmes\CMDBTags> C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1

At C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1:9 char:80

+ ... -Path C:\Scripts\Tags_patch\ressources\List_test.csv -UseCulture | %{

+                                                                         ~

Missing closing '}' in statement block or type definition.

At C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1:27 char:51

+         @{N='IP';E={$_.Guest.IPAddress -join '|'}})

+                                                   ~

Unexpected token ')' in expression or statement.

At C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1:31 char:1

+ }

+ ~

Unexpected token '}' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : MissingEndCurlyBrace

0 Kudos
Leadership
Leadership

I had a typo, code above is ocrrected


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

0 Kudos
Enthusiast
Enthusiast

thank you so much

0 Kudos