DCSIMG
PowerShell to the rescue - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

PowerShell to the rescue


disk A member of my team came in this morning with a scripting request:

"If we ran a full backup on all of our servers (fixed disks), how much disk space would they all consume altogether? We need to give the boss a ‘ball-park number’ by the end of the day!"

So, how can we get that number? Here’s a quick & dirty version.

1. Get all computers objects (names only). We keep our servers in a dedicated OU in active directory.
2. Run a WMI query on all servers and subtract each fixed disk FreeSpace from the total disk size.
3. Use Measure-Object to compute the sum of all of the values from the previous step.
4. Print the result in TB.

$servers = Get-QADComputer -SizeLimit 0 –SearchRoot ‘Domain.com/Servers’ | Select-Object -ExpandProperty Name
$disks = Get-WMIObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $servers -ErrorAction SilentlyContinue | Foreach-Object { $_.Size - $_.FreeSpace }
$totalDiskSpace = $Disks | Measure-Object -Sum
$totalDiskSpace.Sum/1TB

That wasn’t that hard. Needless to say, our boss didn’t have to wait all day long, he got the answer after only a few minutes!


EDIT: I've been asked about a way to get the server names without third party cmdlets.
The following shows how you can use the DirectorySearcher class. We initialize a new DirectorySearcher object and supply to arguments: Filter and PropertiesToLoad. The SearchRoot property tells the searcher where to start the search from (Servers OU).

$searcher = New-Object System.DirectoryServices.DirectorySearcher ‘(&(objectCategory=Computer)(objectClass=User))',’name’
$searcher.SearchRoot= "LDAP://OU=Servers,DC=domain,DC=com"
$servers = $searcher.FindAll() | Foreach-Object { $_.Properties["name"] }

Comments

Jacques Willemen said:

After all successfull Powershell experience over the last 2 years in our company I would not refer to QAD as merely "third" party....

# March 8, 2010 9:45 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: