Automation

 View Only
  • 1.  Import CSV to get VM Disk usage and export results to CSV

    Posted Dec 07, 2023 05:53 PM

    I found something close but trying to import a csv list of VMs to compile disk use stats, and then export this to CSV.

    Similar to this, but not Cluster wide....get VMs only listed\imported csv...

     

    foreach($cluster in Get-CLuster){
        $outputfile = "c:\" + $cluster.Name + "-Diskusage.csv"
        Get-VM -Location $cluster |     Where { $_.PowerState -eq "PoweredOn" } |     Get-VMGuest |     Select VMName -ExpandProperty Disks |     Select VMName, Path,         @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},            @{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},         @{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}} |     Export-Csv -NoTypeInformation $outputfile


  • 2.  RE: Import CSV to get VM Disk usage and export results to CSV

    Posted Dec 07, 2023 05:55 PM

    This was actually from LucD a decade ago    



  • 3.  RE: Import CSV to get VM Disk usage and export results to CSV

    Posted Dec 07, 2023 10:11 PM

    I tweaked LucD's a bit and it is working, except I'd like "HDSizeGB" to be the sum size of all disks for that VM, so allocatedGB?

     

    foreach($VMName in (Get-VM -Name (Import-Csv "C:\Downloads\Servers.csv" | %{$_.VMName}))){
    Get-VM -name $VMName |
    Where { $_.PowerState -eq "PoweredOn" } |
    Get-VMGuest |
    Select VMName -ExpandProperty Disks |
    Select VMName, Path,
    @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},
    @{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},
    @{Name="HDSizeGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},
    @{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}} |
    Export-Csv -NoTypeInformation "c:\Downloads\Disk.csv" -Append -Force
    }



  • 4.  RE: Import CSV to get VM Disk usage and export results to CSV
    Best Answer

    Posted Dec 08, 2023 12:37 PM

    Try with

    foreach ($VMName in (Get-VM -Name (Import-Csv "C:\Downloads\Servers.csv" | ForEach-Object { $_.VMName }))) {
        Get-VM -Name $VMName |
        Where-Object { $_.PowerState -eq "PoweredOn" } |
        Get-VMGuest -PipelineVariable guest |
        Select-Object VMName -ExpandProperty Disks |
        Select-Object VMName, Path,
        @{Name = "DiskCapacityGB"; Expression = { [math]::Round((($_.Capacity) / 1GB), 2) } },
        @{Name = "DiskUsedMB"; Expression = { [math]::Round((($_.Capacity - $_.FreeSpace) / 1MB), 2) } },
        @{Name = "HDSizeGB"; Expression = { [math]::Round((($guest.Disks.Capacity | Measure-Object -Sum).Sum / 1GB), 2) } },
        @{Name = "DiskFreeMB"; Expression = { [math]::Round((($_.FreeSpace) / 1MB), 2) } } |
        Export-Csv -NoTypeInformation "c:\Downloads\Disk.csv" -Append -Force
    }


  • 5.  RE: Import CSV to get VM Disk usage and export results to CSV

    Posted Dec 08, 2023 07:10 PM

    Hi, did you manage to do this ?

     

    Tutuapp



  • 6.  RE: Import CSV to get VM Disk usage and export results to CSV

    Posted Dec 08, 2023 07:17 PM

    Yes, thank you so much LucD!