powershell - 如何在 Powershell 中排除部分输出?
问题描述
我正在编写一个脚本,它接受设备 ID 作为参数来检查磁盘的使用百分比。这是我的代码。
$device_id = $args[0]
Get-WmiObject -Class Win32_LogicalDisk |
Select-Object -Property DeviceID,
@{label='UsedPercentage'; expression={[Math]::Round((($_.Size - $_.FreeSpace)/$_.Size) * 100, 2)}} |
findstr $device_id
这是我的输出。我正在传递一个参数以按设备 ID 查看设备的使用情况。
PS D:\Development\Powershell> .\disk-usage.ps1 D:
D: 57.69
我想做的就是输出那个数字。我该怎么做呢?
解决方案
无需用于findstr
过滤输出。相反,使用参数参数来过滤您的 WMI 查询:
$device_id = $args[0]
# use argument to filter WMI query
Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID = '$device_id'" |ForEach-Object {
# output the free space calculation, nothing else
[Math]::Round((($_.Size - $_.FreeSpace)/$_.Size) * 100, 2)
}
推荐阅读
- javascript - 以编程方式设置选定值 - Mat Select Angular
- javascript - React onClick 适用于重新加载,但不适用于实际点击
- javascript - ag-grid 防止 rowDrop
- javascript - 正则表达式在 Javascript 中获取括号之间和字符串文字内部的字符串
- java - 当我在我的项目库中包含 EclipseLink-ModelGen(JPA 2.1) 时,为什么我的 Java Web 项目会引发错误
- bintray - Bintray 的每晚更新未同步到 JCenter
- php - 有没有办法读取 .env 文件,然后有条件地要求 composer.json 中的包?
- android - 如何理解应该支持哪种架构 apk?
- java - Android 通知未显示用于秒表的 Java
- python - 输入说明