VMware Cloud Community
srinut31
Contributor
Contributor
Jump to solution

How to get VM Present ESX Hostname

Dear Friends,

                  I have Powercli script which get the information about all VM's, how to include  where the VM presented ESX host name and datastore name.

$MyCollection = @()

$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}

ForEach ($VM  ) {

$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can try it like this

$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template}

foreach ($VM in $AllVMs) {
   
$Details = New-Object PSObject
    $Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
   
$Details | Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteProperty
   
$Details | Add-Member -Name Datastore -Value ([string]::Join(',',($vm.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty
   
$MyCollection += $Details
}
$MyCollection



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

View solution in original post

0 Kudos
29 Replies
LucD
Leadership
Leadership
Jump to solution

You can try it like this

$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template}

foreach ($VM in $AllVMs) {
   
$Details = New-Object PSObject
    $Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
   
$Details | Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteProperty
   
$Details | Add-Member -Name Datastore -Value ([string]::Join(',',($vm.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty
   
$MyCollection += $Details
}
$MyCollection



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

0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear LucD,

                 Really Thank very much your quick Reply

0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

LucD,

  But it throws error  like

[vSphere PowerCLI] C:\tmp> .\ESX-Hostname.ps1
Unexpected token 'in' in expression or statement.
At C:\tmp\ESX-Hostname.ps1:1 char:93
+ $AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template}
foreach ($VM in <<<<  $AllVMs) {    $Details = New-Object PSObject    $Details
| Add-Member -Name Name -Value $VM.name -Membertype NoteProperty    $Details |
Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteP
roperty    $Details | Add-Member -Name Datastore -Value ([string]::Join(',',($v
m.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty    $MyCol
lection += $Details}$MyCollection
    + CategoryInfo          : ParserError: (in:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to have lost the <CR><LF> while copying the script.

Attached a correct version


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

0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

LucD,

Can we get the out put in CSV format as its shows the out put

Name                       Host                       Datastore
----                       ----                       ---------
USTPA3IFSWS85              ustpa3ifsvm057.nam.pwci... SYS01_DEV01_CL0811,DAT...

0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear LucD,

                 How can i get string value for datasrote capacity in MB.As you post using that i am getting Datastore name like how can get datastore useage space.

I am new to this how to write string.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Change the last line

$MyCollection

into this

$MyCollection | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try it like this

$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template}

foreach ($VM in $AllVMs) {
    $Details = New-Object PSObject
    $Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
   
$Details | Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteProperty
   
$Details | Add-Member -Name Datastore -Value ([string]::Join(';',($vm.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty     $Details | Add-Member -Name DSCapacity -Value ([string]::Join(';',($vm.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty     $MyCollection += $Details
}
$MyCollection


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

0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

Thanks LucD !! but its genrating Blank report in CSV format.Attache dscript.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You again seem to have <CR><LF> problems.

Use the attached version


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

0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

LucD,

  Thanks once Again its perfectly working.  Smiley Happy

0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear LucD,

                Can you explain about this string where to use "math" and "join", What does mean "{0:f1} "  and wat else can use to get same.

                  ([string]::Join(';',($vm.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)})))

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since there can be more than 1 datastore and since the Export-Csv cmdlet can't handle arrays, I paste the capacity of the different datastores together in 1 string, seperated by a semi-colon.

The [string]::Join function is a .Net function that a PS script can call in this way.

Similarly you can call .Net mathematical functions.

For example the rounding function: [math]::Round($number, 2) which will round the number in $number to 2 decimals.


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

0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

Lucd,

  After executing the script report generated but  Host name Columns showing Blank column.Any need to correct in script.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works without a problem for me.

Can you attach the version of the script you are using ?


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

0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

Please find the Script.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The script is ok, runs fine in my environment and shows the hostnames.

The only explanation I now have is what is written in the SDK "The host that is responsible for running a virtual machine.   This property is null if the virtual machine is not running and is   not assigned to run on a particular host.".

Could it be that all these VMs are powered off  or not assigned to a host ?


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

0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

LucD,

   I tested on other Vcenter2 it pulling the data for Host name too but not on the Vcenter1 which  sent the output thoug this vcenter having 4000 VMs

0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear LucD,

To get Network Conection status i used below string but i am error.

"$Details | Add-Member -Name  Networkstatus -Value   ([string]::Join(',',($vm.Network | %{Get-View $_} | %($_.ConnectionState.Connected)))) -Membertype NoteProperty" is this syntax is correct.

0 Kudos