19 February,2016 by Tom Collins
Retrieving the drive sizes from a server is common administration task. Like many administration task there are loads of different methods to use. Powershell is continiously evolving. One of it's great strengths is the ability to read straight from server APIs , that are are built specifically with Powershell in mind, allowing Powershell to return values with minimal need to manipulate strings.
Powershell also does a great job at allowing existing methods to be utilised using the Powrershell scripting language to manage the workflow.
This is a quick summary of 3 methods I use across different SQL Server management situations. These are starting points
Method 1 : Using the Get-OSDrive cmdlet . It will return drives that are in the current session
Get-PSDrive C | Select-Object Used,Free
Method 2 : Using the Get-WmiObject. There is the added bonus of accessing a remote server via the -computer switch.
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer MyComputer | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f ($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | Out-GridView
Method 3 : SQLCMD invoked by Powershell. The benefit is you can use the SQL Server based commands via the Powershell framework
invoke-expression "SQLCMD -E -S server\myinstance; -q 'exec master.dbo.xp_fixeddrives'"
Expand your Powershell mind – Three key cmdlets - SQL Server DBA
I want to become a DBA. What should I first learn ?
This is only a preview. Your comment has not yet been posted.
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.
Posted by: |