Can anyone tell me how I can go about getting the physical disk id and drive letters of the local server from within management studio? I know I can grab the drive letters with xp_fixed drives and extrapolate to the full disk size with xp_cmdshell and fsutil, but what I want is something like the below:
╔══════════╦═════════════╦════════╦════════════╗
║ Drive_id ║ Dive Letter ║ Size ║ Free_Space ║
╠══════════╬═════════════╬════════╬════════════╣
║ 0 ║ C ║ 10240 ║ 8000 ║
║ 0 ║ E ║ 102400 ║ 25000 ║
║ 1 ║ F ║ 102400 ║ 22000 ║
╚══════════╩═════════════╩════════╩════════════╝
Any ideas?
EDIT:
Just to be clear I have all of the pieces of the puzzle in the below code EXCEPT the physical drive_id. If anyone can think of a way to add it to the code below that would be fantastic – if I need to blow all that away and redo it to get the information I'm after I can live with that too.
Code So Far:
SET NOCOUNT ON
IF OBJECT_ID(N'tempdb..#FreeDiskSize') IS NOT NULL DROP TABLE #FreeDiskSize
CREATE TABLE #FreeDiskSize (name varchar(5), available_mb int)
INSERT #FreeDiskSize(name,available_mb)
EXEC master..XP_FIXEDDRIVES
DECLARE @Drive VARCHAR(5), @CMD VARCHAR(1000), @pos SMALLINT
IF OBJECT_ID(N'tempdb..#TotalDiskSize') IS NOT NULL DROP TABLE #TotalDiskSize
CREATE TABLE #TotalDiskSize (TotalBytes VARCHAR(1000), Drive VARCHAR(5))
DECLARE Drive_name CURSOR FOR
SELECT name FROM #FreeDiskSize
OPEN Drive_name
FETCH NEXT FROM Drive_name INTO @Drive
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD='MASTER..XP_CMDSHELL ' + ''''+ 'FSUTIL VOLUME DISKFREE ' + @Drive + ':| find '+ '"Total # of bytes"'+''''
INSERT #TotalDiskSize(TotalBytes) EXEC (@CMD)
UPDATE #TotalDiskSize SET Drive=@Drive WHERE Drive IS NULL
FETCH NEXT FROM Drive_name INTO @Drive
END
CLOSE Drive_name
DEALLOCATE Drive_name
DELETE FROM #TotalDiskSize WHERE TotalBytes IS NULL
SELECT @pos=charindex(':',TotalBytes) FROM #TotalDiskSize
SELECT b.Drive as Drive, CONVERT(BIGINT,(RIGHT(b.TotalBytes,(LEN(b.TotalBytes)-@pos))))/1073741824 AS TOTAL_Drive_SPACE_GB, a.available_mb/1024 AS AVAILABLE_SPACE_GB
FROM #FreeDiskSize a WITH (NOLOCK)
INNER JOIN #TotalDiskSize b WITH (NOLOCK)ON a.name=b.Drive
Best Answer
You can use glennberry scripts..
Below is the sample output,is this what you need..