Solved

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

Posted on 2014-01-16
12
2,589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

by Nathan Brom/Bromy2004 Introduction There are numerous websites out there for any different type of program you can imagine.  Of those, you'll need to decide which ones are legitimate and aren't trying to steal your money or infect your comput…
This article covers how to install the Microsoft Windows Operating System (OS). What is covered in this article:  > Different Versions and Editions of the Windows OS  > Upgrading versus Fresh Installation of the OS           - Steps to take pr…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
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…

734 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