Automation

 View Only
Expand all | Collapse all

How to get VM Present ESX Hostname

  • 1.  How to get VM Present ESX Hostname

    Posted May 12, 2011 04:39 AM

    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

    }



  • 2.  RE: How to get VM Present ESX Hostname
    Best Answer

    Posted May 12, 2011 05:59 AM

    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




  • 3.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 09:10 AM

    Dear LucD,

                     Really Thank very much your quick Reply



  • 4.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 09:32 AM
      |   view attached

    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

    Attachment(s)

    ps1
    ESX-Hostname.ps1   499 B 1 version


  • 5.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 09:55 AM
      |   view attached

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

    Attached a correct version

    Attachment(s)

    ps1
    ESX-Hostname.ps1   500 B 1 version


  • 6.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 10:41 AM

    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...



  • 7.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 11:14 AM

    Change the last line

    $MyCollection

    into this

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


  • 8.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 10:50 AM

    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.



  • 9.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 11:38 AM

    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


  • 10.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 12:04 PM

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

    Attachment(s)

    txt
    Error-ESXHost.txt   12 KB 1 version
    ps1
    ESX-Hostname2.ps1   766 B 1 version


  • 11.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 12:20 PM
      |   view attached

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

    Use the attached version

    Attachment(s)

    ps1
    ESX-Hostname2-1.ps1   774 B 1 version


  • 12.  RE: How to get VM Present ESX Hostname

    Posted May 12, 2011 12:35 PM

    LucD,

      Thanks once Again its perfectly working.  :smileyhappy:



  • 13.  RE: How to get VM Present ESX Hostname

    Posted May 13, 2011 04:09 AM

    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)})))



  • 14.  RE: How to get VM Present ESX Hostname

    Posted May 13, 2011 05:37 AM

    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.



  • 15.  RE: How to get VM Present ESX Hostname

    Posted May 13, 2011 09:53 AM
      |   view attached

    Lucd,

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

    Attachment(s)

    xls
    Reports.xls   20 KB 1 version


  • 16.  RE: How to get VM Present ESX Hostname

    Posted May 13, 2011 11:44 AM

    Works without a problem for me.

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



  • 17.  RE: How to get VM Present ESX Hostname

    Posted May 13, 2011 12:48 PM
      |   view attached

    Please find the Script.

    Attachment(s)

    ps1
    ESX-Hostname2.ps1   772 B 1 version


  • 18.  RE: How to get VM Present ESX Hostname

    Posted May 13, 2011 01:04 PM

    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 ?



  • 19.  RE: How to get VM Present ESX Hostname

    Posted May 13, 2011 02:30 PM

    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



  • 20.  RE: How to get VM Present ESX Hostname

    Posted May 16, 2011 08:58 AM

    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.



  • 21.  RE: How to get VM Present ESX Hostname

    Posted May 16, 2011 09:51 AM

    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 


  • 22.  RE: How to get VM Present ESX Hostname

    Posted May 16, 2011 10:25 AM

    Dear LucD,

    Thank for your quick replay.

    can you  help me  out  to get VlanID syntax.



  • 23.  RE: How to get VM Present ESX Hostname

    Posted May 16, 2011 11:06 AM

    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


  • 24.  RE: How to get VM Present ESX Hostname

    Posted May 17, 2011 07:04 AM

    Dear Lucd,

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



  • 25.  RE: How to get VM Present ESX Hostname

    Posted May 17, 2011 07:08 AM

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

    I updated the code.



  • 26.  RE: How to get VM Present ESX Hostname

    Posted May 17, 2011 07:32 AM

    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



  • 27.  RE: How to get VM Present ESX Hostname

    Posted May 17, 2011 07:56 AM

    Looks like you don't have the Extensiondata property.

    Which PowerCLI version are you using ?

    Do a

    Get-PowerCLIVersion

    What does this say ?



  • 28.  RE: How to get VM Present ESX Hostname

    Posted May 18, 2011 09:24 AM

    Dear LucD,

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

    VMware vSphere PowerCLI 4.0 U1 build 208462

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



  • 29.  RE: How to get VM Present ESX Hostname

    Posted May 18, 2011 09:56 AM

    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


  • 30.  RE: How to get VM Present ESX Hostname

    Posted May 19, 2011 06:54 AM

    Dear  LucD,

                     Very thankfull for your quick inputs.