Link to home
Start Free TrialLog in
Avatar of globalsupport
globalsupport

asked on

I need powershell script to find unallocated space in disk 0 across hundreds of server

Hi,
    I need a powershell script  that can get me unallocated space in disk 0 from each server ( all the servers are windows 2003) as a part of audit which I have to perform across few hundreds of servers.
Avatar of David
David
Flag of United States of America image

What is your definition of unallocated space?  Do you mean physical blocks of the HDD that are not part of a file once the program (which will take hours) to run?

 If any applications are running that use files on that HDD, then this is a moving target.  NTFS allocates storage for buffer space when a file is opened in read/write mode, or has been written to.  

Or are you looking at something simple like parsing the output of DIR C:\ and looking at the free byte count that will be close, but never exact.
P.S. here is a link to source code for a sophisticated script that should meet your needs. It even bundles it up and sends the results in an email for you.


http://sqlpowershell.wordpress.com/2013/10/01/powershell-script-to-monitor-disk-space-of-a-group-of-servers-html-formatted-email-output/

and another, less bells & whistles but easier to modify
http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/08/use-powershell-to-create-a-report-displaying-free-disk-space.aspx
If you just want free space, here's a quick example of how you could get a table of the free space in GB off the C: drive of a load of PCs in one go.  Put their hostnames in a file called ServerList.txt on your PCs C: drive for this example code.

$servers = Get-Content "C:\ServerList.txt"
$job = Get-WmiObject -ComputerName $servers -Class Win32_LogicalDisk -Filter "DeviceID='C:'" -AsJob
while ($job.State -ne "Completed") {
    Start-Sleep -Seconds 1
}
Receive-Job -Job $job | Select-Object @{L="Hostname";E={$_.PSComputerName}},@{L="Free Space (GB)";E={($_.FreeSpace/1GB) -as [int]}} | Format-Table -AutoSize

Open in new window


I used AsJob above so you could actually enter commands into PowerShell manually and then rather than have a while loop, just receive the job later at your convenience.

If it's truly unallocated space in a drive (ie not assigned to any partition or volume), then someone here has a method that can do that sort of thing by adding up partition sizes on a disk compared to the disk size:

http://social.technet.microsoft.com/Forums/windowsserver/en-US/7ca60bb0-6164-4b4c-954d-c242ba3b4fb1/determine-unpartitioned-space-on-all-hard-disks-of-a-system?forum=winserverpowershell
Avatar of globalsupport
globalsupport

ASKER

@ Dlethe,
                Even though I like the sophisticated script that you have given it doesn't meet my needs as I need only unallocated space of a hard drive. In my organization OS is installed in Disk 0 & C:\ drive & in at least 100 servers I have unallocated space in that hard drive I have the list but I need to know the size that is left unallocated
@ cantoris,
                   Second script is what I also tried but the output is in KB also I don't know how to give multiple computers as input. So I tried a VBscript that was in experts exchange which meets my need, But it works for only one computer at a time if anyone can modify it to take multiple computers as input that would be sufficient. I am pasting the code below
strComputer = "."
Set wmiServices = GetObject("winmgmts:{impersonationLevel=Impersonate}!//" & strComputer)
Set wmiDiskDrives = wmiServices.ExecQuery("SELECT Caption, DeviceID, Size FROM Win32_DiskDrive")
For Each wmiDiskDrive In wmiDiskDrives
      intDiskSpace = Round((wmiDiskDrive.size / 1024 / 1024), 2)
      WScript.Echo wmiDiskDrive.Caption & " (" & wmiDiskDrive.DeviceID & ") " & intDiskSpace & " MB"
      intSpaceLeft = intDiskSpace
      strEscapedDeviceID = Replace(wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare)
      Set wmiDiskPartitions = wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strEscapedDeviceID & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
      For Each wmiDiskPartition In wmiDiskPartitions
            intPartitionSpace = Round((wmiDiskPartition.Size / 1024 / 1024), 2)
            WScript.Echo vbTab & "Partition ID: " & wmiDiskPartition.DeviceID & ", " & intPartitionSpace & " MB"
            intSpaceLeft = Round(intSpaceLeft - intPartitionSpace, 2)
            Set wmiLogicalDisks = wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & wmiDiskPartition.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
            For Each wmiLogicalDisk In wmiLogicalDisks
                  WScript.Echo vbTab & vbTab & "Logical Disk " & wmiLogicalDisk.DeviceID & ", " & (wmiLogicalDisk.Size / 1024 / 1024) & " MB"
            Next
      Next
      WScript.Echo vbTab & "Unpartitioned space: " & intSpaceLeft & " MB"
Next
The script I gave you a pretty HTML report for every computer along with Drive Label, total capacity, used capacity, free space, and free space %.  

The free space IS the unallocated space.  Change the title of the field if you want. If you are complaining about the fact that this does all HDDs instead of just the C drive, then a single IF statement will fix that.
Dlethe,
            I am talking about unallocated space in the image I attached
unallocatedspace.JPG
Then you are asking about something completely different. This represents the total raw HDD capacity minus the size(s) of all defined partitions.

This script can be used as a basis to obtain what you need.
http://gallery.technet.microsoft.com/scriptcenter/Use-Powershell-to-Gather-b5c746d0

Note, if you don't have the skill set to get exactly what you want, then you could probably have somebody crank it out for you if you go to guru.com and offer $20 for a developer to give you exactly what you need as a turnkey solution.
ASKER CERTIFIED SOLUTION
Avatar of cantoris
cantoris
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cantoris,
             This is what i was looking for i modified the script to get the totalspace in GB & as a integer value & it works great. Thanks a ton for your effort.
I'm glad it works for you.
Are you able to readjust your points to give credit to dlethe  who found the script that I tweaked for you?
no I don't know how to do that