0

因此,我正在为我们的服务器编写库存脚本,但在存储方面遇到了障碍。这是代码:

$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

......我无法完全弄清楚这一点。任何人都可以提供帮助吗?

4

2 回答 2

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))
}
于 2013-06-14T16:31:21.120 回答
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 }
于 2014-03-12T06:15:00.337 回答