cul*_*ter 101 powershell scripting powershell-2.0
我有这个单行:
get-WmiObject win32_logicaldisk -Computername remotecomputer
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
DeviceID : A:
DriveType : 2
ProviderName :
FreeSpace :
Size :
VolumeName :
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 20116508672
Size : 42842714112
VolumeName :
DeviceID : D:
DriveType : 5
ProviderName :
FreeSpace :
Size :
VolumeName :
Run Code Online (Sandbox Code Playgroud)
如何获得Freespace和Size的DeviceID C:?我需要提取这两个值而没有其他信息.我用Selectcmdlet 尝试过,但没有效果.
编辑: 我只需要提取数值并将它们存储在变量中.
Sha*_*evy 127
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" |
Select-Object Size,FreeSpace
$disk.Size
$disk.FreeSpace
Run Code Online (Sandbox Code Playgroud)
仅提取值并将其分配给变量:
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" |
Foreach-Object {$_.Size,$_.FreeSpace}
Run Code Online (Sandbox Code Playgroud)
Nul*_*tc. 110
更简单的解决方案:
Get-PSDrive C | Select-Object Used,Free
Run Code Online (Sandbox Code Playgroud)
和远程计算机(需要Powershell Remoting)
Invoke-Command -ComputerName SRV2 {Get-PSDrive C} | Select-Object PSComputerName,Used,Free
Run Code Online (Sandbox Code Playgroud)
gre*_*pit 29
只有一个命令简单甜美,但这只适用于本地磁盘
Get-PSDrive
Run Code Online (Sandbox Code Playgroud)
你仍然可以做一个输入-的PSSession -ComputerName服务器名称远程服务器上使用此命令,然后运行Get-PSDrive来它会拉取数据,如果你从服务器上运行它.
mwe*_*sel 18
我前后创建了一个PowerShell高级功能(脚本cmdlet),允许您查询多台计算机.
该函数的代码长度超过100行,因此您可以在此处找到它:PowerShell版本的df命令
查看" 用法"部分以获取示例.以下用法示例查询一组远程计算机(从PowerShell管道输入)并以表格格式显示输出,其中包含人类可读形式的数值:
PS> $cred = Get-Credential -Credential 'example\administrator'
PS> 'db01','dc01','sp01' | Get-DiskFree -Credential $cred -Format | Format-Table -GroupBy Name -AutoSize
Name: DB01
Name Vol Size Used Avail Use% FS Type
---- --- ---- ---- ----- ---- -- ----
DB01 C: 39.9G 15.6G 24.3G 39 NTFS Local Fixed Disk
DB01 D: 4.1G 4.1G 0B 100 CDFS CD-ROM Disc
Name: DC01
Name Vol Size Used Avail Use% FS Type
---- --- ---- ---- ----- ---- -- ----
DC01 C: 39.9G 16.9G 23G 42 NTFS Local Fixed Disk
DC01 D: 3.3G 3.3G 0B 100 CDFS CD-ROM Disc
DC01 Z: 59.7G 16.3G 43.4G 27 NTFS Network Connection
Name: SP01
Name Vol Size Used Avail Use% FS Type
---- --- ---- ---- ----- ---- -- ----
SP01 C: 39.9G 20G 19.9G 50 NTFS Local Fixed Disk
SP01 D: 722.8M 722.8M 0B 100 UDF CD-ROM Disc
Run Code Online (Sandbox Code Playgroud)
Gre*_*ray 10
另一种方法是将字符串转换为WMI对象:
$size = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'").Size
$free = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'").FreeSpace
Run Code Online (Sandbox Code Playgroud)
如果你想要不同的单位,你也可以将结果除以1GB或1MB:
$disk = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")
"Remotecomputer C: has {0:#.0} GB free of {1:#.0} GB Total" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output
Run Code Online (Sandbox Code Playgroud)
输出是: Remotecomputer C: has 252.7 GB free of 298.0 GB Total
我在其他建议中遇到了两个问题
不受这些问题影响的替代方法是将GetDiskFreeSpaceEx与UNC路径一起使用:
function getDiskSpaceInfoUNC($p_UNCpath, $p_unit = 1tb, $p_format = '{0:N1}')
{
# unit, one of --> 1kb, 1mb, 1gb, 1tb, 1pb
$l_typeDefinition = @'
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
'@
$l_type = Add-Type -MemberDefinition $l_typeDefinition -Name Win32Utils -Namespace GetDiskFreeSpaceEx -PassThru
$freeBytesAvailable = New-Object System.UInt64 # differs from totalNumberOfFreeBytes when per-user disk quotas are in place
$totalNumberOfBytes = New-Object System.UInt64
$totalNumberOfFreeBytes = New-Object System.UInt64
$l_result = $l_type::GetDiskFreeSpaceEx($p_UNCpath,([ref]$freeBytesAvailable),([ref]$totalNumberOfBytes),([ref]$totalNumberOfFreeBytes))
$totalBytes = if($l_result) { $totalNumberOfBytes /$p_unit } else { '' }
$totalFreeBytes = if($l_result) { $totalNumberOfFreeBytes/$p_unit } else { '' }
New-Object PSObject -Property @{
Success = $l_result
Path = $p_UNCpath
Total = $p_format -f $totalBytes
Free = $p_format -f $totalFreeBytes
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
命令行:
powershell gwmi Win32_LogicalDisk -ComputerName remotecomputer -Filter "DriveType=3" ^|
select Name, FileSystem,FreeSpace,BlockSize,Size ^| % {$_.BlockSize=
(($_.FreeSpace)/($_.Size))*100;$_.FreeSpace=($_.FreeSpace/1GB);$_.Size=($_.Size/1GB);$_}
^| Format-Table Name, @{n='FS';e={$_.FileSystem}},@{n='Free, Gb';e={'{0:N2}'-f
$_.FreeSpace}}, @{n='Free,%';e={'{0:N2}'-f $_.BlockSize}},@{n='Capacity ,Gb';e={'{0:N3}'
-f $_.Size}} -AutoSize
Run Code Online (Sandbox Code Playgroud)
输出:
Name FS Free, Gb Free,% Capacity ,Gb
---- -- -------- ------ ------------
C: NTFS 16,64 3,57 465,752
D: NTFS 43,63 9,37 465,759
I: NTFS 437,59 94,02 465,418
N: NTFS 5,59 0,40 1 397,263
O: NTFS 8,55 0,96 886,453
P: NTFS 5,72 0,59 976,562
Run Code Online (Sandbox Code Playgroud)
命令行:
wmic logicaldisk where DriveType="3" get caption, VolumeName, VolumeSerialNumber, Size, FileSystem, FreeSpace
Run Code Online (Sandbox Code Playgroud)
出:
Caption FileSystem FreeSpace Size VolumeName VolumeSerialNumber
C: NTFS 17864343552 500096991232 S01 EC641C36
D: NTFS 46842589184 500104687616 VM1 CAF2C258
I: NTFS 469853536256 499738734592 V8 6267CDCC
N: NTFS 5998840832 1500299264512 Vm-1500 003169D1
O: NTFS 9182349312 951821143552 DT01 A8FC194C
P: NTFS 6147043840 1048575144448 DT02 B80A0F40
Run Code Online (Sandbox Code Playgroud)
命令行:
wmic logicaldisk where Caption="C:" get caption, VolumeName, VolumeSerialNumber, Size, FileSystem, FreeSpace
Run Code Online (Sandbox Code Playgroud)
出:
Caption FileSystem FreeSpace Size VolumeName VolumeSerialNumber
C: NTFS 17864327168 500096991232 S01 EC641C36
command-line:
dir C:\ /A:DS | find "free"
out:
4 Dir(s) 17 864 318 976 bytes free
dir C:\ /A:DS /-C | find "free"
out:
4 Dir(s) 17864318976 bytes free
Run Code Online (Sandbox Code Playgroud)
刚刚找到Get-Volume命令,该命令返回SizeRemaining,因此(Get-Volume -DriveLetter C).SizeRemaining / (1e+9)可以使用类似的命令来查看磁盘 C 的剩余 Gb 大小。似乎比 更快Get-WmiObject Win32_LogicalDisk。
Get-PSDrive C | Select-Object @{ E={$_.Used/1GB}; L='Used' }, @{ E={$_.Free/1GB}; L='Free' }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
551309 次 |
| 最近记录: |