Powershell输出格式问题

时间:2013-06-14 16:25:20

标签: windows powershell powershell-v2.0 windows-server-2008-r2

所以我正在为我们的服务器制作一个库存脚本,并在存储上遇到了障碍。这是代码:

$StorageInfo=Get-WmiObject win32_volume | Where-Object {$_.DriveType -eq 3 -and $_.Label -ne 'System Reserved' -and $_.DriveLetter -ne $null}

$DriveLetters=$StorageInfo | Select-Object DriveLetter | Sort-Object DriveLetter | ft -HideTableHeaders
$DriveNames=$StorageInfo | Sort-Object DriveLetter | Select-Object Label | ft -HideTableHeaders
$DriveCapacity=$StorageInfo |Sort-Object DriveLetter | ForEach-Object {[Math]::Truncate($_.Capacity / 1GB)}

$DriveLetters

$DriveNames

$DriveCapacity

来自的数据如下:

C:                                                                                                                                                                                                  
D:                                                                                                                                                                                                  
E:                                                                                                                                                                                                  
F:                                                                                                                                                                                                  
G:                                                                                                                                                                                                  

OSDisk                                                                                                                                                                                              
Data                                                                                                                                                                                                
SQL Data                                                                                                                                                                                            
SQL Logs                                                                                                                                                                                            
SQL Temp                                                                                                                                                                                            

232
97
97
97
48

我希望能够将其格式化为:

C:\, OSDisk - 232gb
D:\, Data - 97gb
C:\, SQLData - 97gb
C:\, SQLLogs - 97gb
C:\, SQLTemp - 97gb

......我无法理解这一点。有人可以提供帮助吗?

2 个答案:

答案 0 :(得分:2)

尝试这种方式:

Get-WmiObject win32_volume -Filter "DriveType=3 AND Label <> 'System Reserved' AND DriveLetter IS NOT NULL" | 
Select-Object Name,Label,@{Name='CapacityGB';E={[Math]::Truncate($_.Capacity / 1GB)}}

如果您想要所描述的确切输出(感谢Ansgar):

Get-WmiObject win32_volume -Filter "DriveType=3 AND Label <> 'System Reserved' AND DriveLetter IS NOT NULL" | ForEach-Object{
    "{0}, {1} - {2}gb" -f $_.Name,$_.Label,([Math]::Truncate($_.Capacity/1GB))
}

答案 1 :(得分:0)

$logfile = <log file path>
 $path = <path of the file to which we need to sort> 
$Array = gc $path
 $Count = $Array.Count
 for($i=0; $i -le $Count; $i++) 
{ $a =$Array[$i]
 $i = $i+1
 write-output $i | out-file $logfile
 $a+=" "+ $Array[$i] 
$i = $i+1 
write-output $i | out-file  $logfile 
$a+=" "+ $Array[$i]
 write-output $i | out-file $logfile 
$New = $a | Select-Object -Unique #| out-file $logfile }