Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get-DiskUsage.ps1

Status
Not open for further replies.

BikeBoy2020

IS-IT--Management
Feb 1, 2009
5
US
I am trying to modify existing script to list all the drives/luns, including mounts. It outputs ok, BUT... Size is always in 0.000 and so Percent = Infinity.
If I use win32_logicaldisk I get good result, but I need mounts as well, so I need win32_volume.

Any ideas?


param ( [string]$computer = "MyServer" , [switch]$all)
# Formatting
$size = @{ l = "Size (MB)"; e = { $_.size/1mb}; f = "{0:N}"}
$free = @{ l = "free (MB)"; e = { $_.freespace/1mb}; f = "{0:N}"}
$perc = @{ l = "percent"; e = { 100.0 * ([double]$_.freespace/[double]$_.size)}; f="{0:f}" }
$name = @{ e = "name"; f = "{0,-20}" }
$fields = $name,$size,$free,$perc

$filter = "DriveType = '3'"
if ( $all ) { $filter = "" }


get-wmiobject -class win32_volume -filter $filter -comp $computer |
format-table $fields -auto
 
I got the answer, problem is that Size method doesn't work with Win32_Volume, so I am using Capacity instead. It works.
 
Care to post a complete version of the script so that others can use it? You can use CODE tags to preserve formatting.

Pat Richard MVP
Plan for performance, and capacity takes care of itself. Plan for capacity, and suffer poor performance.
 
You bet, here you go:
<CODE>

param ( [string]$computer = $args[0] , [switch]$all)

# Formatting
$size = @{ l = "Size (MB)"; e = { $_.Capacity/1mb}; f = "{0:N}"}
$free = @{ l = "free (MB)"; e = { $_.freespace/1mb}; f = "{0:N}"}
$perc = @{ l = "percent"; e = { 100.0 * ([double]$_.freespace/[double]$_.Capacity)}; f="{0:f}" }

$name = @{ e = "name"; f = "{0,-20}" }
$fields = $name,$size,$free,$perc
$filter = "DriveType = '3'"
if ( $all ) { $filter = "" }

get-wmiobject -class win32_volume -filter $filter -comp $computer |
format-table $fields -a

<\CODE>
 
Is there more to the script? I'm getting an error
Get-WmiObject:Invalid parameter

Code:
#[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1561082[/URL]

param ( [string]$computer = $args[0], [switch]$all)
# Formatting 
$size = @{ l = "Size (MB)"; e = { $_.Capacity/1mb}; f = "{0:N}"}
$free = @{ l = "free (MB)"; e = { $_.freespace/1mb}; f = "{0:N}"} 
$perc = @{ l = "percent"; e = { 100.0 * ([double]$_.freespace/[double]$_.Capacity)}; 
f="{0:f}" }
$name = @{ e = "name"; f = "{0,-20}" }
$fields = $name,$size,$free,$perc
$filter = "DriveType = '3'"
if ( $all ) { $filter = "" }
get-wmiobject -class win32_volume -filter $filter -comp $computer | format-table $fields -a

Pat Richard MVP
Plan for performance, and capacity takes care of itself. Plan for capacity, and suffer poor performance.
 
Ah - okay - weird formatting messed it up. Here it is with the original author's comments added back in.

Code:
# [URL unfurl="true"]http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!138.entry[/URL]
# [URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1561082[/URL]
# Get-DiskUsage.ps1 (aliased to dfspace)
# Use Get-WMIObject to collect disk free info
# Can be used with remote systems

param ( [string]$computer = $args[0], [switch]$all)
# Formatting 
$size = @{ l = "Size (MB)"; e = { $_.Capacity/1mb}; f = "{0:N}"}
$free = @{ l = "free (MB)"; e = { $_.freespace/1mb}; f = "{0:N}"} 
$perc = @{ l = "percent"; e = { 100.0 * ([double]$_.freespace/[double]$_.Capacity)}; f="{0:f}" }
$name = @{ e = "name"; f = "{0,-20}" }
$fields = $name,$size,$free,$perc

# in case the user wants to see more than just local drives
$filter = "DriveType = '3'"
if ( $all ) { $filter = "" }

# go do the work by getting the information from the appropriate
# computer, and send it to format-table with the appropriate 
# fields and formatting info
get-wmiobject -class win32_volume -filter $filter -comp $computer | format-table $fields -a

Pat Richard MVP
Plan for performance, and capacity takes care of itself. Plan for capacity, and suffer poor performance.
 
Pat-

Got the same wmi error but with your code too.. do I actually need to provide my computer name? didn't think so but...

thx
blade
 
Yes. You supply it on the command line... such as
.\Get-DiskUsage.ps1 mycomputer

You can also call it using localhost
.\Get-DiskUsage.ps1 localhost

I probably could clean it up a little.....

Pat Richard MVP
Plan for performance, and capacity takes care of itself. Plan for capacity, and suffer poor performance.
 
Thanks Pat,

Works rather well

Thanks to BikeBoy just as well!!

blade
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top