Solved

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

Posted on 2014-01-16
12
2,223 Views
Last Modified: 2014-01-17
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
Comment
Question by:globalsupport
  • 5
  • 4
  • 3
12 Comments
 
LVL 47

Expert Comment

by:dlethe
ID: 39787520
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
 
LVL 47

Expert Comment

by:dlethe
ID: 39787524
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
 
LVL 16

Expert Comment

by:cantoris
ID: 39788069
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
 

Author Comment

by:globalsupport
ID: 39788398
@ 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
 

Author Comment

by:globalsupport
ID: 39788420
@ 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
 
LVL 47

Expert Comment

by:dlethe
ID: 39788453
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:globalsupport
ID: 39788515
Dlethe,
            I am talking about unallocated space in the image I attached
unallocatedspace.JPG
0
 
LVL 47

Expert Comment

by:dlethe
ID: 39788541
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
 
LVL 16

Accepted Solution

by:
cantoris earned 500 total points
ID: 39788864
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
 

Author Comment

by:globalsupport
ID: 39789117
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
 
LVL 16

Expert Comment

by:cantoris
ID: 39789136
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
 

Author Comment

by:globalsupport
ID: 39789554
no I don't know how to do that
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Hallo! I guess almost every Windows Administrator must have got stumped with this question "Where does WINDOWS store a users cached credentials? Every user who had once logged onto a Server/Desktop while it was connected to the domain could sti…
The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now