PowerShell 按预期增加磁盘大小,但没有任何问题地抛出错误

PowerShell 按预期增加磁盘大小,但没有任何问题地抛出错误

此脚本加载 .csv 文件并遍历虚拟机列表,检查可用磁盘空间,如果低于 GB 的某个数量,则将磁盘空间增加到脚本中指定的数量。如果可用空间已经达到所需数量,它会向控制台写入硬盘空间为指定数量。磁盘大小会发生变化,可以在配置选项卡中看到。大小尚未分配。我为此运行了另一个脚本。我想知道为什么会抛出此错误以及如何解决它。虽然脚本完全按照预期执行,但它给出了以下错误:

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
 

我使用的脚本如下:

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."
}

相关内容