New new disk drives to Windows VMs in VMWare

You may want to add a bunch of new drives to your servers all at once. In my case this was useful for installing a new agent on a large amount of servers. These commands can be used with PowerCLI and WinRM to add new disk drives to the VMs, format them, and mount them as a drive letter in Windows. Then you are all set to use them right away.

Power CLI Commands:

#gather list

#static way
$VMsList = "VM1","VM2"

#dynamic way
$VMsList = (Get-VM | where {$_.Name -match "WIN"}).Name

#add the new disk as 10GB, persistent, and EagerZeroedThick
$VMsList | foreach {
	Get-VM "$($_)" | New-HardDisk -CapacityGB 10 -Persistence persistent -StorageFormat EagerZeroedThick
}

 

Next up you must connect to each Windows VM via PowerShell WinRM and format and mount the drives. This will move the CD ROM drive to F: from D:, initialize all disks as GPT, and then partition them as the D: drive letter.

$VMsList | foreach {
	Invoke-Command -ComputerName "$($_)" {
	
		#move cdrom drive to F: drive from D: drive
		Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" |Set-WmiInstance -Arguments @{DriveLetter='F:'}

		#format all new disks
		Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "" -Confirm:$false

		#setup new Disks, labels, and drive letters
		Get-Disk | foreach {
			
			if (($_.Size -eq 10GB)) {
				Set-Partition -DriveLetter (($_ | Get-Partition).DriveLetter)[1] -NewDriveLetter "D"
			}
		}
	}
}

 

This entry was posted in IT, Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply