Filter Hard Drives by VolumeName

by hibbijibbies at 2013-01-24 12:10:37

Hi,

I am fairly sure that I am asking something trivial, but I have tried hours on end without reaching a solution. We need to do capacity planning on our servers and hence need to add the hard drive total space, free space and % used for 2 volumes named ‘VMs’ and ‘B2D’ for each of a number of servers. Ideally I would like to save this data to a SQL DB from which I can run queries on a monthly basis, but this is beyond the scope of the question and totally beyond my comprehension at this stage.

I can extract the ‘VolumeName’ for one hard drive, but am unable to dot it for more than one hard drive. I need to use the VolumeName as the drive letters are not the same for all servers and the VolumeName is the only common identifier across all servers. So my question is how do I cycle through my servers and only extract the hard drive volumes where they are called either ‘Primary’ and ‘Secondary’, add the data for the 2 volumes together, e.g Total Size, Used, Free, Free % and export the data in a text or .csv file. Hope it is clear what I am trying to achieve Here is my code: It returns an error message.

gwmi Win32_logicaldisk -Filter "VolumeName -eq {‘Main Disk’ -or ‘Primary’}" -computer MyComputerName <br>| Select SystemName,DeviceID,VolumeName,
@{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($.size/1gb))}}, <br>@{Name=&quot;Free Space(GB)&quot;;Expression={[decimal](&quot;{0:N0}&quot; -f($_.freespace/1gb))}},
@{Name="Free Space(%)";Expression={"{0:P2}" -f(($
.freespace/1gb) / ($.size/1gb))}} <br>| ft -auto</blockquote>by ArtB0514 at 2013-01-24 13:38:34<blockquote>Try another tack: use a WQL query instead of a filter. Note: Whenever you have errors in a long pipeline, split it up into logical sections (i.e., collection, processing, presentation) to better debug the process.<br><br><code>$Query = &quot;SELECT * FROM Win32_LogicalDisk WHERE VolumeName=&#39;Main Disk&#39; OR VolumeName=&#39;Primary&#39;&quot;<br>$Disks = Get-WmiObject -Query $Query -ComputerName $MyComputerName<br>$Disks | Format-Table SystemName,DeviceID,VolumeName,<br> @{Name=&quot;Size&#40;GB&#41;&quot;;Expression={&quot;{0:N0}&quot; -f &#40;$_.size/1gb&#41;}},<br> @{Name=&quot;Free Space&#40;GB&#41;&quot;;Expression={&quot;{0:N0}&quot; -f &#40;$_.freespace/1gb&#41;}},<br> @{Name=&quot;Free Space&#40;%&#41;&quot;;Expression={&quot;{0:P2}&quot; -f &#40;&#40;$_.freespace/1gb&#41; / &#40;$_.size/1gb&#41;&#41;}} -AutoSize</code><br><br>You can get help on WQL at <a href="http&#58;//msdn&#46;microsoft&#46;com/en-us/library/aa394606(v=VS&#46;85)&#46;aspx">http&#58;//msdn&#46;microsoft&#46;com/en-us/library/aa394606(v=VS&#46;85)&#46;aspx</a>.</blockquote>by sunnyc7 at 2013-01-24 14:43:22<blockquote>You can try this one.<br>[code2=powershell]gwmi Win32_logicaldisk -Filter &quot;DriveType=3&quot; -computer MyComputerName
| Select SystemName,DeviceID,VolumeName, <br>@{Name=&quot;Size&#40;GB&#41;&quot;;Expression={[decimal]&#40;&quot;{0:N0}&quot; -f&#40;$_.size/1gb&#41;&#41;}},
@{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($
.freespace/1gb))}}, <br>@{Name=&quot;Free Space&#40;%&#41;&quot;;Expression={&quot;{0:P2}&quot; -f&#40;&#40;$_.freespace/1gb&#41; / &#40;$_.size/1gb&#41;&#41;}}
| ft -auto[/code2]
I use this for my inventory:
[code2=powershell]Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer $computer | Select DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($.freespace/1gb) / ($.size/1gb))}} | ft[/code2]
by hibbijibbies at 2013-01-25 00:48:08
Thanks ArtB0514. That gives me the exact result I am looking for. I am quite new to Powershell and other Microsoft technologies and didn’t even know of the existence of WQL. Thanks for your time and advice. I appreciate it.