powershell - 如何将文本添加到PowerShell表结果中?

标签 powershell get-wmiobject

我试图弄清楚如何在PowerShell表的结果中添加一些文本,例如MHz或GB。 foreach循环可能吗?

Get-WmiObject Win32_PhysicalMmory |
    Format-Table Banklabel, Manufacturer,
        @{Label="Speed"; Expression={$_.ConfiguredClockSpeed}},
        @{Label="Capacity"; Expression={[int64]($_.Capacity/1GB)}} -AutoSize

Result 1

我有另一种接近的方法,但是由于它不会将信息添加到新行中,因此无法使用多个结果。
$Disk = Get-WmiObject -Class Win32_LogicalDisk
$DiskSpace = [int64]($Disk.Size/1GB)
$FreeSpace = [INT64]($Disk.FreeSpace/1GB)
$UsedSpace = (($DiskSpace-$FreeSpace)/$DiskSpace).ToString("P0")
$OBJ = New-Object PSObject
$OBJ | Add-Member NoteProperty "ID" ($Disk.DeviceID)
$OBJ | Add-Member NoteProperty "Name" ($Disk.VolumeName)
$OBJ | Add-Member NoteProperty "Format" ($Disk.FileSystem)
$OBJ | Add-Member NoteProperty "Capacity" ("$DiskSpace-GB")
$OBJ | Add-Member NoteProperty "Free Space" ("$FreeSpace-GB")
$OBJ | Add-Member NoteProperty "Used" ($UsedSpace)
Write-Output $OBJ | Format-Table

Result 2

最佳答案

根据您的要求使用串联。

更换

Get-WmiObject win32_physicalmemory | Format-Table Banklabel, Manufacturer, @{Label="Speed";` Expression={$_.ConfiguredClockSpeed}}, @{Label="Capacity";` Expression={[INT64]($_.Capacity/1GB)}} -autosize

有了这个:
Get-WmiObject win32_physicalmemory | Format-Table Banklabel, Manufacturer, @{Label="Speed";` Expression={"$($_.ConfiguredClockSpeed) MHz"}}, @{Label="Capacity";` Expression={"$([INT64]($_.Capacity/1GB)) GB"}} -autosize

希望能帮助到你。

关于powershell - 如何将文本添加到PowerShell表结果中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259703/

相关文章:

powershell - 在 PowerShell 中合并 `Get-Disk` 信息和 `LogicalDisk` 信息(但具有格式化输出)

powershell - 从远程计算机获取特殊文件夹

PowerShell:如何使用 WMI 在远程计算机上运行 powershell 脚本

azure - 使用 Azure Active Directory 用户时,远程 WMI 查询失败并显示 'A security package specific error occurred'

.net - 无法在 PowerShell 中创建 .Net 对象

powershell - 运行 Enable-Migrations 时出现错误

python - conda init powershell 没有效果

Powershell 启动进程、超时等待、杀死并获取退出代码

windows - 获取 WMIObject 速度属性错误 (PowerShell)

powershell - 使用Windows截图并在cmd中草绘剪贴板临时图像