0

So I'm working on an inventory script for our servers and have hit a roadblock at the storage. Here's the code:

$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

The data that comes from that is as follows:

C:                                                                                                                                                                                                  
D:                                                                                                                                                                                                  
E:                                                                                                                                                                                                  
F:                                                                                                                                                                                                  
G:                                                                                                                                                                                                  

OSDisk                                                                                                                                                                                              
Data                                                                                                                                                                                                
SQL Data                                                                                                                                                                                            
SQL Logs                                                                                                                                                                                            
SQL Temp                                                                                                                                                                                            

232
97
97
97
48

I'd like to be able to format it as such:

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

...and I can't quite figure this out. Can anyone offer assistance?

2 Answers 2

2

Try this way:

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)}}

If you want the exact output as described (thanks 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))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Use "{0}\, {1} - {2}" -f (DriveLetter,Label,@{Name='...}) instead of Select-Object ... to get the particular output format.
I'll be working on this again at the end of this week. I'll let you know how it goes then. Thanks for the responses, guys!
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 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.