This script loads a .csv file and iterates through a list of VM's and checks free disk space and if below a certain amount in GB, increases disk space to that specified in the script. If the free space is already the desired amount, it writes to the console that hard disk space is a specified amount. The disk size changes and can be seen in the configure tab. The size is not allocated yet. I run another script for that. I would like to know why this is throwing this error and how I can remedy it. While the script does exactly what it is supposed to do, it gives an error as follows:
Resize-Partition : Size Not Supported
Extended information:
The partition is already the requested size.
Activity ID: {7f77f10d-2ae4-43d6-b6f1-ddbff4778f0d}
At line:44 char:1
+ Resize-Partition -DriveLetter $drive_letter -Size $size.SizeMax
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (StorageWMI:ROOT/Microsoft/.../MSFT_Partition) [Resize-Partition], CimException
+ FullyQualifiedErrorId : StorageWMI 4097,Resize-Partition
The script I use is below:
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" |
Select-Object -Property DeviceID, DriveType, VolumeName,
@{L='FreeSpaceGB';E={"{0:N2}" -f ($_.FreeSpace /1GB)}},
@{L="Capacity";E={"{0:N2}" -f ($_.Size/1GB)}}
if (((Get-Volume -DriveLetter C).Size)/1GB -lt 80)
{
Write-Host "Hard Disk Space is less than 60."##Task Change Disk Size
##Variable clear
$csvobjects = @()
$cskobject = @()
$network = @()
$isalive = @()
#Get VM's to add disk space to
#$GetFreeSpace = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}
#Export to CSV
#$dateTime | Export-Csv -NoTypeInformation -Path "D:\_Tools\PowerShell_CLI_HardDriveExpand-DiskFreeSpace-$(Get-Date -Format "yyyyMMddHHmmssff").csv" -Append
##Import VM name(s)
$csvobjects = Import-CSV -path C:\Temp\CSK-11519DISK-1.csv
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
connect-viserver -server anyuser.anydomain.com -User [email protected]
foreach ($csvobject in $csvobjects){
##Variable clear
$network = @()
$isalive = @()
##Pre-change data gathering
$beforechange = (GET-VM -Name $csvobject.vmname | FT -auto CapacityGB|out-string)
##Stop VM
GET-VM -Name $csvobject.vmname | Get-HardDisk -Name 'Hard disk 1' | Set-HardDisk -CapacityGB 80 -Confirm:$false
start-sleep -s 60
# Variable specifying the drive you want to extend
$drive_letter = "C"
# Script to get the partition sizes and then resize the volume
$size = (Get-PartitionSupportedSize -DriveLetter $drive_letter)
Resize-Partition -DriveLetter $drive_letter -Size $size.SizeMax
}
}
else
{
Write-Host "Hard Disk Space is greater than 60."
}