header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Disk Usage Across Servers
Last Post 30 Sep 2011 08:42 AM by eidgenosse. 27 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SynJunkieUser is Offline
Basic Member
Basic Member
Posts:126
Avatar

--
01 Aug 2008 07:21 AM

    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

    Mark E. SchillUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    01 Aug 2008 04:01 PM

    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
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    02 Aug 2008 04:58 AM

    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

     

    Mark E. SchillUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    02 Aug 2008 04:26 PM
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    02 Aug 2008 11:13 PM


    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://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    SynJunkieUser is Offline
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    07 Aug 2008 04:34 AM

    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
    Advanced Member
    Advanced Member
    Posts:565
    Avatar

    --
    07 Aug 2008 06:04 AM

    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
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    13 Aug 2008 04:56 AM

    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
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    13 Aug 2008 05:04 AM
    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
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    13 Aug 2008 05:10 AM

    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

    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    13 Aug 2008 05:35 AM

    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://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    SynJunkieUser is Offline
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    14 Aug 2008 06:09 AM
    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
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    14 Aug 2008 11:03 PM

    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://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    SynJunkieUser is Offline
    Basic Member
    Basic Member
    Posts:126
    Avatar

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

    --
    27 Aug 2008 08:27 AM
    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

    StuUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    09 Oct 2008 05:10 AM

    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
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    24 Oct 2008 12: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:2
    Avatar

    --
    24 Dec 2008 07:33 AM
    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
    Advanced Member
    Advanced Member
    Posts:565
    Avatar

    --
    24 Dec 2008 08:08 AM
    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/20...ilization/
    Community Director, PowerShellCommunity.org
    Co-host, PowerScripting Podcast
    Author, TechProsaic
    ScottUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    12 Mar 2010 08:26 PM
    I'm a total newbie to Powershell and to this forum. Just wanted to take a minute to say I learned a ton from this thread and I took bits and pieces to accomplish a script that I've been working on.Good work SynJunkie!
    StanUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    11 Jan 2011 08:25 AM
    When i attempt to use this command to find the disk space on our servers I keep running into this error.

    "Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument
    that is not null or empty and then try the command again."

    I am supplying a fully populated text file. If I run the get-content command by itself against the text file it produces the list of servers in the text file. Any ideas as to why this keeps failing? Here is the entire string that I am attempting to use.

    gwmi -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FROM win32_logicaldisk WHERE DriveType=3
    " -computer (gc c:\scripts\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)}}
    StanUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    11 Jan 2011 09:09 AM
    On a side note this command works properly when supplying a server name after the -computer switch.

    gwmi -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FROM win32_logicaldisk WHERE DriveType=3
    " -computer server1 | Select-Object SystemName,Caption,VolumeName,@{Name="Size(GB)"; Expression={"{0
    :N2}" -f ($_.Size/1GB)}},@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freespace/1GB)}}
    AlbertWTUser is Offline
    New Member
    New Member
    Posts:29
    Avatar

    --
    13 Feb 2011 02:39 PM
    Posted By swasserman on 11 Jan 2011 10:09 AM
    On a side note this command works properly when supplying a server name after the -computer switch.

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

    Hi man,

    your script doesn't work ?

    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\Temp\c0e1ac37-8e65-4a57-9271-506f0a0546b1.ps1:1 char:5
    + gwmi <<<<  -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FROM win32_logicaldisk WHERE DriveType=3" -computer server1 | Select-Object Syste
    mName,Caption,VolumeName,@{Name="Size(GB)"; Expression={"{0:N2}" -f ($_.Size/1GB)}},@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freespace/1GB)}}
        + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
        + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
     
    CywebitUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    14 Mar 2011 09:12 AM
    Take a closer look at your text file. Try it with just 1 server name in there, make sure you have NO spaces after it. Also, you can run into problems saving in notepad. Check your encoding during the 'Save As', you might try UTF-8 instead of ANSI.
    mserranoUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    23 Aug 2011 03:21 AM
    Hello,
    I have the same problem as Stan.
    Can you please tell me the solution, i have tried everything but don´t works

    Thanks

    My error:
    PS C:\test> gwmi -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FRO
    M win32_logicaldisk WHERE DriveType=3" -computer (gc c:\test\serverlist.txt) | S
    elect-Object SystemName,Caption,VolumeName,@{Name="Size(GB)"; Expression={"{0:N2
    }" -f ($_.Size/1GB)}},@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freesp
    ace/1GB)}}, @{n="% Free";e={"{0:P2}" -f ([long]$_.FreeSpace/[long]$_.Size)}} | s
    ort "% Free" | export-csv c:\test\Disk-GB1.csv
    Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum
    ent is null or empty. Supply an argument that is not null or empty and then try
    the command again.
    At line:1 char:117
    + gwmi -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FROM win32_l
    ogicaldisk WHERE DriveType=3" -computer <<<< (gc c:\test\serverlist.txt) | Sel
    ect-Object SystemName,Caption,VolumeName,@{Name="Size(GB)"; Expression={"{0:N2}
    " -f ($_.Size/1GB)}},@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freesp
    ace/1GB)}}, @{n="% Free";e={"{0:P2}" -f ([long]$_.FreeSpace/[long]$_.Size)}} |
    sort "% Free" | export-csv c:\test\Disk-GB1.csv
    + CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindi
    ngValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
    Shell.Commands.GetWmiObjectCommand
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1089
    Avatar

    --
    23 Aug 2011 05:41 AM
    Does this work?

    GC c:\test\serverlist.txt | %{gwmi -query "SELECT SystemName,Caption,VolumeName,Size,Freespace FRO
    M win32_logicaldisk WHERE DriveType=3" -computer $_ | S
    elect-Object SystemName,Caption,VolumeName,@{Name="Size(GB)"; Expression={"{0:N2
    }" -f ($_.Size/1GB)}},@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freesp
    ace/1GB)}}, @{n="% Free";e={"{0:P2}" -f ([long]$_.FreeSpace/[long]$_.Size)}} | s
    ort "% Free"} | export-csv c:\test\Disk-GB1.csv

    If that does not work, then I would do 2 things. First I would make the loop display the name of the machine that it is trying to reach each time. Also I would take that whole mess apart and rebuild it using more explicit structure instead of the pipes and all the hidden implicit iteration that comes with them. I love to use pipes at the command prompt, but just like aliases I think that their over use makes actual scripts harder to work with.
    "Look Ma...no strings!"
    bartadaUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    26 Sep 2011 06:24 AM
    I am writing mine for an email alert if the servers have space issues. I am having a problem getting the sort to work properly. It is not sorting the Freespace(GB) properly.  It seems it is only sorting on the first character. Here is the script:

    $output = gwmi Win32_Volume -filter "DriveType=3" -comp (gc \\servername\pc\scripts\powershell\servers.txt) | select SystemName, Caption, @{Name="Size(GB)";Expression={"{0:N1}" -f ($_.capacity/1gb)}}, @{Name="Freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb) }}, @{Name="% Free";Expression={[int64](([int64]$_.Freespace / [int64]$_.capacity) * 100) }} | sort "Freespace(GB)" | fl | out-string

    Does anyone have a solution for this?
    eidgenosseUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    30 Sep 2011 08:42 AM
    I don't have a solution, but the problem is that as it is, FreeSpace(GB) is a string. Maybe it's possible to force a conversion in the sort?

    I had a related problem trying to import the results into a table using the SqlBulkloader as the destination column was a bigint and wouldn't accept the commas from the "{0:N2}" string format. I eventually found that "{0:#}" seemed to serve both my purposes of truncating the non-integer bits from the decimal and give me the number in a non-commarised format. (Any suggestions on how to get decimal(18,2) precision without the nasty commas, gratefully accepted).

    Anyway, cut a long story short, with the above code from Shay, and a function to output to a Date table courtesy of SqlServerCentral, I was able to import the data straight into my sql table.

    My question is, how would I add a date column to the output? Add-member?
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer