• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3330
  • Last Modified:

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.
0
globalsupport
Asked:
globalsupport
  • 5
  • 4
  • 3
1 Solution
 
DavidPresidentCommented:
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.
0
 
DavidPresidentCommented:
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
0
 
cantorisCommented:
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
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
globalsupportAuthor Commented:
@ 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
0
 
globalsupportAuthor Commented:
@ 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
0
 
DavidPresidentCommented:
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.
0
 
globalsupportAuthor Commented:
Dlethe,
            I am talking about unallocated space in the image I attached
unallocatedspace.JPG
0
 
DavidPresidentCommented:
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.
0
 
cantorisCommented:
Here's a tweaked version of the PowerShell:

$computers = Get-Content "C:\Servers.txt"
foreach ($computer in $computers) {
    $disks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $computer
    $partitions = Get-WmiObject -Class Win32_DiskPartition -ComputerName $computer
    $unpartitioned = 0
    $inUse = 0
    $partitionCount = 0
    foreach ($disk in $disks) {
        foreach ($partition in $partitions) {
            if ($partition.DiskIndex -eq $disk.Index) {
                $partitionCount++
                $inUse += $partition.Size
            }
        }
        $obj = New-Object -TypeName PSObject
        $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer
        $obj | Add-Member -MemberType NoteProperty -Name DiskIndex -Value $disk.Index
        $obj | Add-Member -MemberType NoteProperty -Name TotalSpace -Value $disk.Size 
        $obj | Add-Member -MemberType NoteProperty -Name PartitionCount -Value $partitionCount
        $obj | Add-Member -MemberType NoteProperty -Name UnallocatedSpace -Value ((($disk.Size - $inUse) / 1GB) -as [int])
        Write-Output $obj
        $obj = $null
        $inUse=0
        $partitionCount=0
    }
}

Open in new window

I can't fully test it as none of my drives have unallocated space.
0
 
globalsupportAuthor Commented:
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.
0
 
cantorisCommented:
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?
0
 
globalsupportAuthor Commented:
no I don't know how to do that
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now