PowerCLI

 View Only
  • 1.  List VM properties with VMware PowerCLI

    Posted Apr 21, 2014 03:53 AM

    Looking for a script which provides exports the output in the following format (CSV), I did look for some sample script in the forum but the output is different to what I am currently looking  

    VM Attribute

    VM Name

    VM’s with snapshot

    Vm1, vm2

    VM’s with tools outdate

    Vm5,vm10

     

    I have got few more properties to add on to the report, if I can get an example to start with I can give a try.



  • 2.  RE: List VM properties with VMware PowerCLI

    Posted Apr 21, 2014 08:39 AM

    You can use the following example to create the desired report. The code is easily expandable with other properties.

    $vm = Get-VM

    # VM’s with snapshot

    $vms = $vm |

      Where-Object {$_ | Get-Snapshot} |

      Select-Object -ExpandProperty Name

    if ($vms) {$vms = [string]::Join(',', $vms)}

    [pscustomobject]@{

      "VM Attribute" = "VM’s with snapshot"

      "VM Name" = $vms

    }

    # VM’s with tools outdate

    $vms = $vm |

      Where-Object {$_.ExtensionData.Summary.Guest.ToolsStatus -ne 'OK'} |

      Select-Object -ExpandProperty Name

    if ($vms) {$vms = [string]::Join(',', $vms)}

    [pscustomobject]@{

      "VM Attribute" = "VM’s with tools outdate"

      "VM Name" = $vms

    }



  • 3.  RE: List VM properties with VMware PowerCLI

    Posted Apr 21, 2014 11:19 AM

    The given script is producing the output in the below format

    name value
    VM AttributeVM's with snapshot
    VM Namevm1, vm5,etc..
    VM AttributeVM's with tools outdate
    VM Namevm2,vm3,etc..

    But I am looking for output as below in a CSV file

    VM Attribute

    VM Name

    VM’s with snapshot

    Vm1, vm2

    VM’s with tools outdate

    Vm5,vm10



  • 4.  RE: List VM properties with VMware PowerCLI

    Posted Apr 21, 2014 08:25 PM



    This should work for you:


    $AllReport = @()



    Get-VM | foreach {



    $vm = $_



    $vmReport = "" | Select Name, @{ N="Tools Status"; E={ $vm.extensiondata.guest.toolsStatus -match "Old" } }, @{ N="Snapshot count"; E={ $vm | where { $vm | Get-Snapshot } } }



    $vmReport.Name = $vm.name



    $vmReport."Tools Status" = $vm.extensiondata.guest.toolsStatus



    $vmReport."Snapshot count" = ($vm | Get-Snapshot).count



    $AllReport += $vmReport



    Write $vmReport





    $AllReport | Export-Csv -NoTypeInformation "c:\Foldername\filename.csv" #CHANGE ME




  • 5.  RE: List VM properties with VMware PowerCLI

    Posted Apr 21, 2014 08:31 PM


    Sorry, the code was cut off.



    $AllReport = @()



    Get-VM | foreach {



    $vm = $_



    $vmReport = "" | Select Name, @{ N="Tools Status"; E={ $vm.extensiondata.guest.toolsStatus -match "Old" } },



     @{ N="Snapshot count"; E={ $vm | where { $vm | Get-Snapshot } } }



    $vmReport.Name = $vm.name



    $vmReport."Tools Status" = $vm.extensiondata.guest.toolsStatus



    $vmReport."Snapshot count" = ($vm | Get-Snapshot).count



    $AllReport += $vmReport





    $AllReport | Export-Csv -NoTypeInformation "c:\Foldername\filename.csv" #CHANGE ME




  • 6.  RE: List VM properties with VMware PowerCLI

    Posted Apr 22, 2014 01:15 AM

    I am using a similar script, but the output which I am looking is different to what which is being currently produced.. Check the previous post for the output I am currently looking..

    Thanks!!



  • 7.  RE: List VM properties with VMware PowerCLI

    Posted Apr 22, 2014 07:20 AM

    My previous script is using the [pscustomobject] typecast which is a PowerShell 3.0 feature. You are probably using PowerShell 2.0. I modified the script using the New-Object cmdlet, to make it PowerShell 2.0 compatible.

    $vm = Get-VM

    # VM’s with snapshot

    $vms = $vm |

      Where-Object {$_ | Get-Snapshot} |

      Select-Object -ExpandProperty Name

    if ($vms) {$vms = [string]::Join(',', $vms)}

    New-Object -TypeName PSObject -Property @{

      "VM Attribute" = "VM’s with snapshot"

      "VM Name" = $vms

    }

    # VM’s with tools outdate

    $vms = $vm |

      Where-Object {$_.ExtensionData.Summary.Guest.ToolsStatus -ne 'OK'} |

      Select-Object -ExpandProperty Name

    if ($vms) {$vms = [string]::Join(',', $vms)}

    New-Object -TypeName PSObject -Property @{

      "VM Attribute" = "VM’s with tools outdate"

      "VM Name" = $vms

    }