powershell 在Windows 2012命令行中查找所有驱动器卷上的可用空间(GB)

z9smfwbn  于 2022-12-13  发布在  Shell
关注(0)|答案(2)|浏览(167)

如何使用命令行和/或powershell在windows 2012服务器上以GB为单位找到所有活动驱动器卷上的可用空间。
到目前为止我试过这个命令,那失败了:

wmic logicaldisk get size,freespace,caption
Invalid GET Expression.

我也试过这个命令,但它给出的答案是字节,这是很难解释。如果我可以得到这个读取GB:

PS C:\Users\us-tdunphy> wmic diskdrive list brief /format:list

Caption=AWS PVDISK SCSI Disk Device
DeviceID=\\.\PHYSICALDRIVE1
Model=AWS PVDISK SCSI Disk Device
Partitions=1
Size=268432012800

Caption=AWS PVDISK SCSI Disk Device
DeviceID=\\.\PHYSICALDRIVE0
Model=AWS PVDISK SCSI Disk Device
Partitions=1
Size=128849011200

我还尝试了这个powershell命令,它给出了一个错误:

PS C:\Users\us-tdunphy> powershell -command "& {Get-WmiObject -Class Win32_LogicalDisk -Filter 'DriveType = 3' |select PSComputerName, Caption,@{N='Capacity_GB'; E={[math]::Round(($_.Size / 1GB), 2)}},@{N='FreeSpace_GB'; E={[math]::Round(($_.FreeSpace / 1GB), 2)}},@{N='PercentUsed'; E={[math]::Round(((($_.Size - $_.FreeSpace) / $_.Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round((($_.FreeSpace / $_.Size) * 100), 2) }}}"
At line:1 char:277
+ ... - .FreeSpace) / .Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round(((.Free ...
+                    ~
You must provide a value expression following the '/' operator.
At line:1 char:278
+ ...  .FreeSpace) / .Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round(((.FreeS ...
+                    ~~~~~
Unexpected token '.Size' in expression or statement.
At line:1 char:277
+ ... - .FreeSpace) / .Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round(((.Free ...
+                    ~
Missing closing ')' in expression.
At line:1 char:238
+ ... ercentUsed'; E={[math]::Round((((.Size - .FreeSpace) / .Size) * 100), 2) }},@{N= ...
+                    ~
Missing closing '}' in statement block.
At line:1 char:294
+ ... Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round(((.FreeSpace / .Size) *  ...
+                    ~
The hash literal was incomplete.
At line:1 char:3
+ & {Get-WmiObject -Class Win32_LogicalDisk -Filter 'DriveType = 3' |select PSComp ...
+   ~
Missing closing '}' in statement block.
At line:1 char:294
+ ... Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round(((.FreeSpace / .Size) *  ...
+                    ~
Unexpected token ')' in expression or statement.
At line:1 char:296
+ ... ze) * 100), 2) }},@{N='PercentFree'; E={[math]::Round(((.FreeSpace / .Size) * 10 ...
+                    ~
Unexpected token '}' in expression or statement.
At line:1 char:297
+ ... e) * 100), 2) }},@{N='PercentFree'; E={[math]::Round(((.FreeSpace / .Size) * 100 ...
+                    ~
Unexpected token '}' in expression or statement.
At line:1 char:370
+ ... ) * 100), 2) }}}
+                    ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
dsekswqp

dsekswqp1#

试着编码你的命令,CMD不喜欢那些带管道的长字符串:

$command = 'Get-WmiObject -Class Win32_LogicalDisk | Select-Object DeviceID,VolumeName,@{N="SizeGB";E={[Math]::Round($_.Size /1gb,1)}},@{N="FreeSpaceGB";E={[Math]::Round($_.FreeSpace /1gb,1)}}'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand
8i9zcol2

8i9zcol22#

您可以尝试以下方法开始工作:

GWMI Win32_LogicalDisk |
    % {
        "DeviceID:  $($_.DeviceID)"
        "FreeSpace: $($_.FreeSpace / 1GB)GB"
    }

相关问题