Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How can i get all the servers disk space in to one table


How can i get all the servers disk space in to one table

Author
Message
New persopn
New persopn
SSC Veteran
SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)

Group: General Forum Members
Points: 227 Visits: 650
Hi All,

here i need some help and how can i get all the server disk space information in one table.

here we have to run the job and it has to load the disk information in to table.

how can i create it using powershell scripts and batch files.
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10305 Visits: 9586
I don't know about getting it into a table with power shell and batch files, but here's an approach that would work in SQL. It'll fire the fsutil DOS command for every drive from a: to z:. If you want to fire this for a known list of drives instead, you could probably modify it. This does require that you can use xp_cmdshell and the SELECT statement could use a little tweaking.

  DECLARE @intDrive Integer,
@strSQL Varchar(100);

SET @intDrive = 97;

DECLARE @Drives TABLE (
Drive Char(1),
Info Varchar(80));

WHILE @intDrive <= 122
BEGIN
SET @strSQL = 'execute xp_cmdshell ''fsutil volume diskfree ' + CHAR(@intDrive) + ':''';
INSERT @Drives(Info)
EXEC (@strSQL);

UPDATE @Drives
SET Drive = CHAR(@intDrive)
WHERE Drive IS NULL;

SET @intDrive = @intDrive + 1;
END;

SELECT Drive, TotalBytes, FreeBytes
FROM (SELECT Drive,
SUM(CASE WHEN Info LIKE 'Total # of bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS Bigint) ELSE CAST(0 AS Bigint) END) TotalBytes,
SUM(CASE WHEN Info LIKE 'Total # of free bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS Bigint) ELSE CAST(0 AS Bigint) END) FreeBytes,
SUM(CASE WHEN Info LIKE 'Total # of avail free bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS Bigint) ELSE CAST(0 AS Bigint) END) AvailFreeBytes
FROM (SELECT Drive, Info
FROM @Drives
WHERE Info LIKE 'Total # of %') useless_alias
GROUP BY Drive) useless_alias_2
ORDER BY Drive;




Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
New persopn
New persopn
SSC Veteran
SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)

Group: General Forum Members
Points: 227 Visits: 650
if i run the above command i am not getting any out put and all the columns are blank.
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10305 Visits: 9586
What if you run it on the C: drive only? What is your output here?

execute xp_cmdshell 'fsutil volume diskfree c:';




Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
spaghettidba
spaghettidba
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5732 Visits: 13305
If you're on 2008R2 or 2012 you can use sys.dm_os_volume_stats to query disk information.
It would look something like this:


SELECT DISTINCT d.logical_volume_name
,d.volume_mount_point
,CAST(d.total_bytes / 1024 / 1024.0 AS INT) AS TotalMB
,CAST(d.available_bytes / 1024 / 1024.0 AS INT) AS FreeMB
FROM sys.master_files f
CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) d



One small caveat: it extracts information only for the disks that contain a data or log file.

Hope this helps
Gianluca

--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
New persopn
New persopn
SSC Veteran
SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)

Group: General Forum Members
Points: 227 Visits: 650
SELECT DISTINCT d.logical_volume_name
,d.volume_mount_point
,CAST(d.total_bytes / 1024 / 1024.0 AS INT) AS TotalMB
,CAST(d.available_bytes / 1024 / 1024.0 AS INT) AS FreeMB
FROM sys.master_files f
CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) d

---------------------------------------------------

Now above query is working. example if we have 5 servers i need all the servers disk space information in one query.
spaghettidba
spaghettidba
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5732 Visits: 13305
Powershell will do the trick:


sl c:\
$qry = "
SELECT DISTINCT d.logical_volume_name
,d.volume_mount_point
,CAST(d.total_bytes / 1024 / 1024.0 AS INT) AS TotalMB
,CAST(d.available_bytes / 1024 / 1024.0 AS INT) AS FreeMB
FROM sys.master_files f
CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) d
"

$servers = ("SERVER1","SERVER2","SERVER3","SERVER4","SERVER5")

$results = @()

$servers | ForEach-Object {
$data = Invoke-Sqlcmd -ServerInstance $_ -Query $qry
$data | ForEach-Object {
$results += $_
}
}

$results



--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
robert.sterbal 56890
robert.sterbal 56890
Old Hand
Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)

Group: General Forum Members
Points: 300 Visits: 1077
This update of the script outputs 2 formatted columns in addition to the raw numbers:


  DECLARE @intDrive Integer,
@strSQL Varchar(100);

SET @intDrive = 97;

DECLARE @Drives TABLE (
Drive Char(1),
Info Varchar(80));

WHILE @intDrive <= 122
BEGIN
SET @strSQL = 'execute xp_cmdshell ''fsutil volume diskfree ' + CHAR(@intDrive) + ':''';
INSERT @Drives(Info)
EXEC (@strSQL);

UPDATE @Drives
SET Drive = CHAR(@intDrive)
WHERE Drive IS NULL;

SET @intDrive = @intDrive + 1;
END;

SELECT Drive,
convert(varchar, cast((left(TotalBytes,len(TotalBytes)-2)) as money)/1000000000, 1) 'Total Space (GB)',
convert(varchar, cast((left(FreeBytes,len(FreeBytes)-2)) as money)/1000000000, 1) 'Free Space (GB)',
TotalBytes,
FreeBytes
FROM (SELECT Drive,
SUM(CASE WHEN Info LIKE 'Total # of bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS Bigint) ELSE CAST(0 AS Bigint) END) TotalBytes,
SUM(CASE WHEN Info LIKE 'Total # of free bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS Bigint) ELSE CAST(0 AS Bigint) END) FreeBytes,
SUM(CASE WHEN Info LIKE 'Total # of avail free bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS Bigint) ELSE CAST(0 AS Bigint) END) AvailFreeBytes
FROM (SELECT Drive, Info
FROM @Drives
WHERE Info LIKE 'Total # of %') useless_alias
GROUP BY Drive) useless_alias_2
ORDER BY Drive;


Perry Whittle
Perry Whittle
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8784 Visits: 16558
Use this rather than fsutil

wmic volume get name,capacity,"free space"



-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs" ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search