Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

How to create a Powershell Function to return server disk capacity

13 April,2016 by Tom Collins

Question: Is there an efficient way to return the aggregate of the various drives on a Windows OS using Powershell? Instead of returning individual drives , I need to a total capacity .

Ideally, this would be a function which I can use in different powershell scripts .

Answer:This Powershell function accepts an input parameter of a server name. The script uses Get-Wmiobject method, to iterate though every disk – note: I have it filtered at DriveType=3 – you can change this to your specifications.

The script aggregates the disks and uses the $TotalCapacity variable, to return to the calling process. There is an example code to call the function.  

 

Function GetAggDiskCapacity([string]$server)
{
$computer = $server
$disks = @(Get-WmiObject Win32_LogicalDisk -ComputerName $computer -Filter 'DriveType = 3')
 $TotalCapacity = 0
$disks | ForEach { $TotalCapacity += $_.Size }
return $TotalCapacity 
    
}
##example

##GetAggDiskCapacity myservername45


Read more on Powershell scripts

Powershell Scripts for DBA (SQL Server DBA)

Powershell - run script on all sql servers (SQL Server DBA)

Free disk space on drives using Powershell (SQL Server DBA)

 


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to create a Powershell Function to return server disk capacity


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer