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

}

Reply
0 Kudos
29 Replies
LucD
Leadership
Leadership
Jump to solution

I would do that like this

$Details | Add-Member -Name Networkstatus -Value ([string]::Join(',', (Get-VIObjectByVIView -MoRef $vm.MoRef | Get-NetworkAdapter | %{$_.ConnectionState.Connected}))) -Membertype NoteProperty 


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

srinut31
Contributor
Contributor
Jump to solution

Dear LucD,

Thank for your quick replay.

can you  help me  out  to get VlanID syntax.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You better use the Get-VM instead of the Get-View in that case.

The script becomes something like this

$MyCollection = @()
$AllVMs = Get-VM | where {-not $_.Extensiondata.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.Extensiondata.Runtime.Host).Name -Membertype NoteProperty
   
$Details | Add-Member -Name Datastore -Value ([string]::Join(';',($vm.Extensiondata.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty
   
$Details | Add-Member -Name DSCapacity -Value ([string]::Join(';',($vm.Extensiondata.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty
   
$Details | Add-Member -Name Networkstatus -Value ([string]::Join(',', (Get-NetworkAdapter -VM $vm | %{$_.ConnectionState.Connected}))) -Membertype NoteProperty
   
$Details | Add-Member -Name VlanId -Value ([string]::Join(',', (Get-VirtualPortGroup -VM $vm | %{$_.VlanId}))) -Membertype NoteProperty
   
$MyCollection += $Details
}
$MyCollection


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

Reply
0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear Lucd,

Where did you get the "$vmImpl" var from ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, that was a leftover from a prevous version of the script.

I updated the code.


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

Reply
0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear Lucd,

Script Which posted i excuted on our setup, From that i able to get the of Name, VlanID and NetworkStatus,  But not able to get the values of Hostname and DSCapacity.

###################   ERROR ########################

Exception calling "Join" with "2" argument(s): "Value cannot be null.
Parameter name: value"
At C:\test.ps1:10 char:66
+     $Details | Add-Member -Name DSCapacity -Value ([string]::Join <<<< (';',($vm.Extensiondata.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\test.ps1:9 char:54
+     $Details | Add-Member -Name Host -Value (Get-View <<<<  $vm.Extensiondata.Runtime.Host).Name -Membertype NoteProperty    $Details | Add-Member -Name Datastore -Value ([string]::Join(';',($
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\test.ps1:10 char:112
+     $Details | Add-Member -Name DSCapacity -Value ([string]::Join(';',($vm.Extensiondata.Datastore | %{Get-View <<<<  $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView

Exception calling "Join" with "2" argument(s): "Value cannot be null.
Parameter name: value"
At C:\test.ps1:10 char:66

#############################################

The out put

Name          : Windows 2003 Template
Networkstatus : False
VlanId        : 0

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you don't have the Extensiondata property.

Which PowerCLI version are you using ?

Do a

Get-PowerCLIVersion

What does this say ?


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

Reply
0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear LucD,

#######################################

VMware vSphere PowerCLI 4.0 U1 build 208462

#########################################

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That explains it, you can install PowerCLI 4.1U1 and then the previous script will work.

Or you can use the following variation on the script (which will also work with PowerCLI 4.0U1).

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

foreach ($VM in $AllVMs) {
    $vmImpl = Get-VIObjectByVIView -MORef $vm.MoRef
    $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     $Details | Add-Member -Name Networkstatus -Value ([string]::Join(',', (Get-NetworkAdapter -VM $vmImpl | %{$_.ConnectionState.Connected}))) -Membertype NoteProperty     $Details | Add-Member -Name VlanId -Value ([string]::Join(',', (Get-VirtualPortGroup -VM $vmImpl | %{$_.VlanId}))) -Membertype NoteProperty     $MyCollection += $Details
}
$MyCollection


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

Reply
0 Kudos
srinut31
Contributor
Contributor
Jump to solution

Dear  LucD,

                 Very thankfull for your quick inputs.

Reply
0 Kudos