Get free/used disk space on all our VMs for reporting purposes

Mounir 0 Reputation points
2024-11-15T15:10:22.89+00:00

Hi,

Within our corporate environment, we have hundreds of Windows Azure VMs deployed to users via Citrix DaaS.

For monitoring purposes, we would like to see free/used space on C drive. The goal is to have a reports showing disk space status on C drive for all VMs using PowerShell.

So far we were able to get disk capacity only.

Please help/advise.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,336 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,056 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,634 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sai Krishna Katakam 1,005 Reputation points Microsoft Vendor
    2024-11-15T17:27:45.85+00:00

    Hi Mounir,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    To retrieve free/used disk space on all your Azure VMs, you can use PowerShell and Azure's Invoke-AzVMRunCommand. This script allows you to execute commands remotely on Azure VMs and collect the required disk space data. Below are detailed steps:

    Steps to Get Free/Used Disk Space for All VMs

    Enable WinRM on the VM

    Before running the PowerShell script, ensure that Windows Remote Management (WinRM) is enabled on your VMs. Open PowerShell on each VM and run:

    Enable-PSRemoting -Force
    

    This command configures the VM to accept remote PowerShell commands.

    Install the Azure PowerShell Module (if not already installed):

    Install-Module -Name Az -AllowClobber -Scope CurrentUser
    

    Script to Retrieve Disk Space

    The following script iterates through all VMs in a specified resource group, executes a command to check disk space on the C: drive, and outputs the results.

    # Define the resource group and target VMs
    $ResourceGroupName = "<YourResourceGroupName>"  # Replace with your resource group name
    $Command = @'
    Get-PSDrive -Name C | Select-Object @{Name="Used (GB)";Expression={"{0:N2}" -f ($_.Used / 1GB)}}, 
        @{Name="Free (GB)";Expression={"{0:N2}" -f ($_.Free / 1GB)}}, 
        @{Name="Total (GB)";Expression={"{0:N2}" -f (($_.Used + $_.Free) / 1GB)}}, 
        @{Name="Used Percentage";Expression={"{0:N2}" -f ($_.Used / ($_.Used + $_.Free) * 100)}}
    '@
    # Get all VMs in the resource group
    $VMs = Get-AzVM -ResourceGroupName $ResourceGroupName
    foreach ($VM in $VMs) {
        $VMName = $VM.Name
        $Result = Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptString $Command
        Write-Output "Results for VM: $VMName"
        Write-Output $Result.Value
    }
    

    The script will display the following details for the C: drive of each VM:

    Used Space (GB), Free Space (GB), Total Space (GB), Used PercentageUser's image

    If an answer has been helpful, please consider accept the answer and "Upvote" to help increase visibility of this question for other members of the Microsoft Q&A community. 

    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.