Get-PartitionSupportedSize 失败并出现错误

Get-PartitionSupportedSize is failing with error

这是我正在使用的命令:

for $disk in Get-Disk
    $driveLetter = (Get-Partition -DiskNumber $disk.Number | where {$_.DriveLetter}).DriveLetter
    $partitionNum = (Get-Partition -DriveLetter $driveLetter).PartitionNumber
    $allowedSize = (Get-PartitionSupportedSize -DiskNumber $disk.Number -PartitionNumber $partitionNum).SizeMax
    Write-verbose "Total Partition Size allowed: $allowedSize"

这是堆栈:

Get-PartitionSupportedSize : Failed
Activity ID: {a5e65922-521b-46c4-aac8-047ea73a3790}
At C:\extendPartition.ps1:30 char:25
+ ... owedSize = (Get-PartitionSupportedSize -DiskNumber $disk.Number -Part ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (StorageWMI:ROOT/Microsoft/.../MSFT_Partition) [Get-PartitionSupportedSize], CimException
    + FullyQualifiedErrorId : StorageWMI 4,Get-PartitionSupportedSize

找不到相同的任何好的链接,因此不胜感激。

编辑:很抱歉遗漏了一些东西。所以我修改了我的代码以显示我正在尝试的内容。如您所见,我试图只打印 allowedSize。 接下来函数 Get-PartitionSupportedSize 在 Windows 10 Virtual Machine.

上失败

一些小东西
这对我有用

$PartitionNum=3 #or whatever You need 
  foreach ($disk in Get-Disk)
        {
            $allowedSize = (Get-PartitionSupportedSize -DiskNumber $disk.Number -PartitionNumber $partitionNum).SizeMax
        }

但是:

  • $allowedSize 将随着循环的每一轮被覆盖
  • $partitionNum 定义一次。如果你想遍历所有分区你可能想做一些像
    Get-Partition |select -Property PartitionNumber, DriveLetter, type ,@{n='sizemax';e={($_|Get-PartitionSupportedSize).sizemax}}, @{n='sizemin';e={($_|Get-PartitionSupportedSize).sizemin}}

既然有人给了你修复代码的答案,我想我应该给你作品,减去分区类型。尝试将 link 分区类型从 get-PartitionSupportedSize.

调整为文件大小有点困难

输入

$AllowedSize = @()

Foreach($Disk in (Get-Disk)){
    $Output = (get-PartitionSupportedSize -DiskNumber $disk.Number)
    If($Output.count -gt 1){
        Foreach($HDD in $Output){
            $HDD.SizeMax = ([math]::round($HDD.sizemax/1GB, 2));$HDD.SizeMin = ([math]::round($HDD.sizemin/1GB, 2))
            $Output | Add-Member -NotePropertyName FriendlyName -NotePropertyValue $disk.FriendlyName -Force
        }
    } Else {
        $Output.SizeMax = ([math]::round($Output.sizemax/1GB, 2));$Output.SizeMin = ([math]::round($Output.sizemin/1GB, 2))
        $Output | Add-Member -NotePropertyName FriendlyName -NotePropertyValue $disk.FriendlyName -Force
    }
    $AllowedSize += $Output
}
$AllowedSize

输出

SizeMin SizeMax FriendlyName             
------- ------- ------------             
    0.1     0.1 NVMe Samsung SSD 960     
   0.44    0.44 NVMe Samsung SSD 960     
   0.02    0.02 NVMe Samsung SSD 960     
 461.26  465.21 NVMe Samsung SSD 960     
   3.05  111.79 Samsung SSD 840 EVO 120GB
   0.12    0.12 ST2000DM006-2DM164       
1169.37 1662.89 ST2000DM006-2DM164       
 130.03     200 ST2000DM006-2DM164

Set-Service -Name defragsvc -StartupType Manual

解决问题。