Powershell script to fetch the storage capacity of VMware VM

Share
Powershell script to fetch the storage capacity of VMware VM

This guide helps you find out how much storage different VMware virtual machines (VMs) have. It uses a special script. This script looks at a file called vmlist.txt where all the VM names are written. Then, it checks each of these VMs to see how much storage they use. After checking, it puts all this information into a file named output.csv. This way, you can easily see the storage details of all the listed VMs in one place. This script makes it easier to keep track of storage space for lots of VMs. It’s like having a quick way to gather and organize information about how much storage each VM is using in VMware.

Detailed Powershell Script :

# Load necessary VMware modules
Import-Module -Name VMware.PowerCLI
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

# Configure PowerCLI settings to handle certificates
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

# Connect to the VMware server using its name or IP address
Connect-VIServer “VcenterName or IP”

# Retrieve a list of servers from the specified file
$serverList = Get-Content “C:Tempvmlist.txt”

# Set the file path for storing the output
$outputFile = ‘C:Tempoutput.csv’

# Initialize an empty array for storing server information
$vmServerList = @()

# Retrieve information for each VM and store it
$Output = foreach ($server in $serverList) {
Get-VM $server | Select-Object Name, PowerState, UsedSpaceGB, ProvisionedSpaceGB
$vmServerList += $server
}

# Export the collected information to a CSV file
$Output | Export-Csv -Path ‘C:Tempoutput.csv’ -NoTypeInformation

This script performs the following tasks:

  1. Loads the necessary VMware modules for PowerShell.
  2. Sets up PowerCLI configuration to handle certificate issues without confirmation.
  3. Connects to a VMware server using its name or IP address.
  4. Reads a list of servers from the “vmlist.txt” file located at “C:Temp”.
  5. Specifies the file path for storing the script’s output as “output.csv” in “C:Temp”.
  6. Creates an empty array to hold server information.
  7. Retrieves details (Name, PowerState, UsedSpaceGB, ProvisionedSpaceGB) for each VM listed in the file.
  8. Appends the server information to the $Output variable.
  9. Exports the collected information into a CSV file located at “C:Tempoutput.csv” without including type information.

 

Powershell code :

Import-module -Name vmware.powercli
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer 10.0.50.100

$serverList = Get-Content “C:\Users\vmlist.txt”
$outputFile = ‘C:\Users\output.csv’

$vmServerList = @()

$Output = foreach ($server in $serverList)
{
Get-VM $server | select Name, PowerState, UsedSpaceGB, ProvisionedSpaceGB
$vmServerList+=$server
}

$Output | Export-Csv C:\Users\output.csv -NoTypeInformation

 

 

In conclusion, this script simplifies VMware VM management by efficiently gathering storage information. If you found this post helpful, consider sharing it with friends and colleagues passionate about scripting. Sharing knowledge fosters a stronger community and helps others streamline their tasks. Feel free to spread the word among those who would benefit from these scripting insights.

Share this :


Facebook


Twitter


Youtube

VMware Mastery

Hi, I've been working with Windows, VMware and Cloud technologies for more than 12 years. I love delving into the exciting realm of technology and enjoy sharing my knowledge with others. I write about IT and Technology, covering both technical and non-technical topics.

Leave a Reply