如何使用命令行和/或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
由于
答案 0 :(得分:0)
你可以尝试这个开始工作:
GWMI Win32_LogicalDisk |
% {
"DeviceID: $($_.DeviceID)"
"FreeSpace: $($_.FreeSpace / 1GB)GB"
}
答案 1 :(得分:0)
尝试对命令进行编码,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