PowerCLI

  • 1.  Script to export VMs each disk data (size, free space)

    Posted Feb 04, 2025 03:07 AM

    Hi,

    I'm not so proficient in building the Powershell/PowerCLI scripts. I would like to create one that:

    - Imports PowerCli module
    - Connects to the vCenter
    - Lists each hard disk for each VM including disk name (C, D, E, etc), disk capacity and free space
    - Then exports the formatted table in CSV where each virtual machine's data is in a single line and disks details are in separate columns

    I've been trying to find a good way to do so but I failed. Can anyone help with a good solution?



  • 2.  RE: Script to export VMs each disk data (size, free space)

    Posted Feb 04, 2025 03:10 AM

    I'm afraid there is no foolproof method to link a VMDK to a Guest OS partition.



    ------------------------------


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


    ------------------------------



  • 3.  RE: Script to export VMs each disk data (size, free space)

    Posted Feb 04, 2025 09:40 AM

    @LucD I see, thank you. . What kind of useful data can be then exported and combined with "Hard Disk 1", "Hard Disk 2" except the disk capacity? There's no way to export any free disk space data as it's an OS related one?


    I'd just like to create a script that can:

    - Import PowerCli module
    - Connect to the vCenter
    - List VM names, IP addresses, OS, CPU usage, RAM usage and some Disks details
    - Then export the formatted table in CSV where each virtual machine and its data is in a single line

    Can you suggest how such script should look like? Any help would be appreciated.




  • 4.  RE: Script to export VMs each disk data (size, free space)

    Posted Feb 04, 2025 11:16 AM

    If you installed the PowerCLI modules in a folder mentioned in $env:PSModulePath, you don't need to explicitly load the modules.


    To export some of the data you are looking for you could do something like this

    Connect-VIServer -Server vcsa.domain -User 'user' -Password 'VMware1!'
    
    Get-VM |
    Select Name,
      @{N='IP';E={$_.Guest.IPAddress -join '|'}},
      @{N='OS';E={$_.Guest.OSFullName}},
      @{N='CPUUsage%';E={(Get-Stat -Entity $_ -Stat 'cpu.usage.average' -Realtime -MaxSamples 1).Value}},
      @{N='MemoryUsage%';E={(Get-Stat -Entity $_ -Stat 'mem.usage.average' -Realtime -MaxSamples 1).Value}},
      @{N='HardDisk1GB';E={(Get-HardDisk -VM $_ -Name 'Hard disk 1').CapacityGB}},
    @{N = 'HardDisk2GB'; E = { (Get-HardDisk -VM $_ -Name 'Hard disk 2').CapacityGB }} |
    Export-Csv -Path .\report -NoTypeInformation -UseCulture
    



    ------------------------------


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


    ------------------------------