我在csv文件中有以下数据
"Server","Disk ###","Status","Size","Free"
"s1","Disk 0","Online","100 GB","1024 KB"
"s1","Disk 1","Online","6144 MB","1024 KB"
"s1","Disk 2","Online","200 GB","1024 KB"
"s1","Disk 3","Online","10 GB","1024 KB"预期:
"Server","Disk ###","Status","Size","Free", "Total Size"
"s1","Disk 0","Online","100 GB","1024 KB","100 GB"
"s1","Disk 1","Online","6144 MB","1024 KB","60 GB"
"s1","Disk 2","Online","200 GB","1024 KB","100 GB"
"s1","Disk 3","Online","10 GB","1024 KB","2.44 TB"我想从Get-Disk命令中添加总大小列。
但我无法选择列
我试过像下面这样使用
Get-Disk | Select-Object Total SizeGet-Disk | Select-Object 'Total Size'但它提供的是空白输出。
请告诉我是怎么回事
试过如下
$dataa= Get-Disk | Select-Object -Property @{n="TotalSize";e={'{0:n2} GB' -f ($_.Size / 1GB)}} | ft
Import-Csv -path c:\infile.csv |
Select-Object *,@{Name='TotalSize';Expression={$dataa}} |
Select-Object "Server","Disk ###","Status","Size","Free", "TotalSize" |
Export-Csv -path C:\Outfile.csv -NoTypeInformation获取数据,如
"Server","Disk ###","Status","Size","Free","TotalSize"
"s1","Disk 0","Online","100 GB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"
"s1","Disk 1","Online","6144 MB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"
"s1","Disk 2","Online","200 GB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"
"s1","Disk 3","Online","10 GB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"发布于 2021-10-01 09:49:00
当Get-Disk输出时,它会执行一些格式化以显示在一个很好的布局中。但是,它实际上并不具有默认输出中的所有属性。
要查看实际存在的属性,请运行:
Get-Disk 0 | Format-List *您可能会看到的唯一与大小相关的属性是AllocatedSize和Size。如果您查看磁盘CIM类 Get-Disk返回的内容,您将看到AllocatedSize是“磁盘上当前使用的空间的数量(以字节为单位)”。Size是“磁盘的总大小,以字节为单位”。
因此,要获得类似于您的输出,您需要如下所示:
Get-Disk |
Select-Object -Property @{n='Server';e={hostname}},
@{n='Disk ###';e={'Disk {0}' -f $_.Number}},
@{n='Status';e={$_.OperationalStatus}},
@{n='Size';e={'{0:n2} GB' -f ($_.AllocatedSize / 1GB)}},
@{n='Free';e={'{0:n2} GB' -f (($_.Size - $_.AllocatedSize)/1GB)}},
@{n='TotalSize';e={'{0:n2} GB' -f ($_.Size / 1GB)}} |
ConvertTo-Csv -NoTypeInformationhttps://stackoverflow.com/questions/69403540
复制相似问题