Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Checking disk space usage


This recipe shows how to list disks available for your SQL Server instance, how much is used, and how much is available.

How to do it...

Follow these steps to check the disk space usage:

  1. Open PowerShell ISE as administrator.

  2. Add the following script and run:

    #get server list
    $servers = @("localhost")
    
    #this can come from a file instead of hardcoding
    #the servers
    #servers = Get-Content <filename>
    
    Get-WmiObject -ComputerName $servers -Class Win32_Volume |
    ForEach-Object {
       $drive = $_
       $item  = [PSCustomObject] @{
          Name = $drive.Name
          DeviceType = switch ($drive.DriveType)
                       {
                        0 {"Unknown"}
                        1 {"No Root Directory"}
                        2 {"Removable Disk"}
                        3 {"Local Disk"}
                        4 {"Network Drive"}
                        5 {"Compact Disk"}
                        6 {"RAM"}
                       }
          SizeGB = "{0:N2}" -f ($drive.Capacity/1GB)
          FreeSpaceGB = "{0:N2...