header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Disk Usage Across Servers
Last Post 24 Dec 2008 05:08 PM by halr9000. 18 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
01 Aug 2008 04:21 PM  

Hello

 

I've created a pretty basic script to go to a bunch of severs listed in a file (servers.txt) and report back to a csv file the disk size and freespace.  My problem is i'm very new to powershell and I am really struggling to get both the disk size AND the freespace converted to GB.  I can do one or the other but not both.

 

Any advice would be greatly appreciated.

 

Here's the script.

Gwmi win32_logicaldisk -Filter "DriveType=3" -comp (gc c:\servers.txt) | Select-Object -Property SystemName,Caption,VolumeName,Size,FreeSpace | ForEach-Object -Process {$_.FreeSpace = ($_.FreeSpace)/1024/1024/1024; $_} | format-table | out-file -filepath c:\Disk-GB.csv

 

 Many thanks

Lee

meson3902User is Offline
New Member
New Member
Posts:12
Avatar

--
02 Aug 2008 01:01 AM  

Try this:

Gwmi win32_logicaldisk -Filter "DriveType=3" -comp (gc c:\servers.txt) | Select-Object SystemName,Caption,VolumeName,@{Name="Size"; Expression={$_.Size/1GB}},@{Name="Freespace"; Expression={$_.Freespace/1GB}} | format-table | out-file -filepath c:\Disk-GB.csv

 

SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
02 Aug 2008 01:58 PM  

Thanks for help.

I can see the changes you have made but i'm not entirely sure how what the @ does in this instance. If it's no problem, could you explain how it fixes the script so i can learn my mistake.

Thanks

 

meson3902User is Offline
New Member
New Member
Posts:12
Avatar

--
03 Aug 2008 01:26 AM  
See if this helps:

http://www.microsoft.com/technet/scriptcenter/resources/pstips/apr08/pstip0425.mspx
ShayUser is Offline
Basic Member
Basic Member
Posts:271
Avatar

--
03 Aug 2008 08:13 AM  


1. This below WMI query is processed on the client itself (aka server side processing) saving lots of network bandwidth and is much faster to execute in terms of script performance.

2. There is no need to format-table when you want to store the output in csv files, instead use the export-csv cmdlet.

PS > gwmi -query "SELECT SystemName,Caption,VolumeName FROM win32_logicaldisk WHERE DriveType=3" -computer (gc c:\servers.txt) | Select-Object SystemName,Caption,VolumeName,@{Name="Size"; Expression={$_.Size/1GB}},@{Name="Freespace"; Expression={$_.Freespace/1GB}} | export-csv -filepath c:\Disk-GB.csv

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
07 Aug 2008 01:34 PM  

Thanks all for the help and suggestions.  The script is looking real good and i've added  | sort -prop Freespace | into the pipeline to get things in order.

 

One thing I want to add to the script to get it perfect is to get PowerShell to create a new field which is the freespace percentage.  Currently I do this by hand once its in the spreadsheet which works but isn't ideal.

Any ideas on this would be welcomed.

Thanks

Lee

 

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
07 Aug 2008 03:04 PM  

You can use Add-Member to add that property and simple division of course to generate the value.

$freespace = $obj.diskUsage / $obj.diskCapacity
$obj | add-member -name FreeSpace -MemberType NoteProperty -Value $freespace
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
13 Aug 2008 01:56 PM  

hmmm.

I seem to get stuck on this, I tried it a couple ways around but with no luck.

First this way:

$freespace = $obj.diskUsage / $obj.diskCapacity;
$obj | add-member -name FreeSpace -MemberType NoteProperty -Value $freespace;
Gwmi win32_logicaldisk -Filter "DriveType=3" -comp (gc c:\server.txt) | Select-Object SystemName,Caption,VolumeName,@{Name="Size"; Expression={$_.Size/1GB}},@{Name="Freespace"; Expression={$_.Freespace/1GB}} | sort -property Freespace | format-table

and then this way:


Gwmi win32_logicaldisk -Filter "DriveType=3" -comp (gc c:\server.txt) | Select-Object SystemName,Caption,VolumeName,@{Name="Size"; Expression={$_.Size/1GB}},@{Name="Freespace"; Expression={$_.Freespace/1GB}} | sort -property Freespace | format-table;
$freespace = $obj.diskUsage / $obj.diskCapacity;
$obj | add-member -name FreeSpace -MemberType NoteProperty -Value $freespace;

 

The error I always get is:

Attempted to divide by zero.
At line:1 char:30
+ $freespace = $obj.diskUsage /  <<<< $obj.diskCapacity;

 

 

Sorry to be a pain,

 

Lee

SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
13 Aug 2008 02:04 PM  
Posted By Shay on 08/03/2008 12:13 AM


1. This below WMI query is processed on the client itself (aka server side processing) saving lots of network bandwidth and is much faster to execute in terms of script performance.

2. There is no need to format-table when you want to store the output in csv files, instead use the export-csv cmdlet.

PS > gwmi -query "SELECT SystemName,Caption,VolumeName FROM win32_logicaldisk WHERE DriveType=3" -computer (gc c:\servers.txt) | Select-Object SystemName,Caption,VolumeName,@{Name="Size"; Expression={$_.Size/1GB}},@{Name="Freespace"; Expression={$_.Freespace/1GB}} | export-csv -filepath c:\Disk-GB.csv

 

Thanks Shay, i have just tested the speed of this compared to my first effort and you were right, it's loads faster.

 

Thanks for the suggestion, I think in future i'll bear this in mind.

 

Regards

Lee

SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
13 Aug 2008 02:10 PM  

Sorry Shay, further to my last reply, to get the script to work I had to lose the -path option and then it run real fast.  However, when I just checked the output csv the values for diskspace and freespace were all 0.

 

Regards

 

Lee

ShayUser is Offline
Basic Member
Basic Member
Posts:271
Avatar

--
13 Aug 2008 02:35 PM  

Sorry Lee, my bad. I pasted an incomplete  wmi query. Here's a modifed version of the complete command (one line):  

gwmi -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FROM win32_logicaldisk WHERE DriveType=3" -computer (gc c:\servers.txt) | Select-Object SystemName,Caption,VolumeName,@{Name="Size(GB)"; Expression={"{0:N2}" -f ($_.Size/1GB)}},@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freespace/1GB)}} | export-csv c:\Disk-GB.csv

 

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
14 Aug 2008 03:09 PM  
Shay, that is awesome, it works perfectly and is so much faster than my original query. Now i need to get the additional column in for Freespace % that Hal was helping me with.

Thank you so much for helping with this. I'm going to pick through the command and make sure I understand it.

Thanks again

Lee
ShayUser is Offline
Basic Member
Basic Member
Posts:271
Avatar

--
15 Aug 2008 08:03 AM  

Add this to your select-object statment, it adds a new column ('% free') to the output

@{n="% Free";e={"{0:P2}" -f ([long]$_.FreeSpace/[long]$_.Size)}}

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
15 Aug 2008 11:56 PM  
Thanks Shay, i'm cooking with gas now!
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
27 Aug 2008 05:27 PM  
I just wanted to post up my finalised script now that it has been perfected with much of the work done by you guys. I appreciate your help i've learned alot.

The script essentially takes a list of servers from a file called servers.txt saved in c:\ and lists the Server Name, the drive, the volume name, the total disk size in GB, the freespace in GB and the % freespace. and finally the list is sorted on the % freespace column and exported out to a csv file called Disk-GB in the root of C:\.

gwmi -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FROM win32_logicaldisk WHERE DriveType=3" -computer (gc c:\servers.txt) | Select-Object SystemName,Caption,VolumeName,@{Name="Size(GB)"; Expression={"{0:N2}" -f ($_.Size/1GB)}},@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freespace/1GB)}}, @{n="% Free";e={"{0:P2}" -f ([long]$_.FreeSpace/[long]$_.Size)}} | sort "% Free" | export-csv c:\Disk-GB.csv

sta4402User is Offline
New Member
New Member
Posts:1
Avatar

--
09 Oct 2008 02:10 PM  

THank you for the detail. Very nice!  However, my serverlist.txt contains servers that either may not be accessible or are in another domain and not reachable with the credentials that i am logged in with. If the script cannot reach a server, it fails and there is no c:\disk-GB.csv generated. How can i get the scipt to keep going even though the server may not exist or is not reachable? THank you.

SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
24 Oct 2008 09:58 AM  
You could wrap the script up into a function and then add a ping function before it so any hosts that do not respond to a ping are not queried.
bartadaUser is Offline
New Member
New Member
Posts:1
Avatar

--
24 Dec 2008 04:33 PM  
I want a different script to append to the original csv a free space column so i can run this every week and see disk space trends. Anyone ever do that before? I have been struggling with it all week. I have tried using out-file but it just puts it at the bottom and not a new column. Any help would be great.
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
24 Dec 2008 05:08 PM  
Rather than append to the original CSV it would be much easier to generate a new file with the freespace already included. Much much easier. btw here's a handy script for disk usage: http://blog.sapien.com/index.php/2008/08/01/powershell-driveutilization/
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer