SQL Clone
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
Say Hey Kid
Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)

Group: General Forum Members
Points: 689 Visits: 657
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
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50223 Visits: 10844
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
Say Hey Kid
Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)

Group: General Forum Members
Points: 689 Visits: 657
if i run the above command i am not getting any out put and all the columns are blank.
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50223 Visits: 10844
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
Gianluca Sartori
Gianluca Sartori
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24441 Visits: 13362
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
Say Hey Kid
Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)Say Hey Kid (689 reputation)

Group: General Forum Members
Points: 689 Visits: 657
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.
Gianluca Sartori
Gianluca Sartori
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24441 Visits: 13362
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
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2416 Visits: 1236
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
SSC Guru
SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)

Group: General Forum Members
Points: 55212 Visits: 17709
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