扩展 xp_fixeddrives

 

xp_fixeddrvies提供磁盘分区的信息太少,如下的脚本提供磁盘分区的使用情况,运行需要管理员权限!
if not exists( select * from sys.configurations(nolock) cc 
 where cc.name='xp_cmdshell' 
 and cc.value_in_use=1)
 begin
    exec sp_configure 'show advanced options',1
    reconfigure

    exec sp_configure 'xp_cmdshell',1
    reconfigure
 end

if OBJECT_ID('tempdb..#fixeddrives_temp') is not null
drop table #fixeddrives_temp;
Go


if OBJECT_ID('tempdb..#totaldrives_temp') is not null
drop table #totaldrives_temp;
Go

create table tempdb..#fixeddrives_temp(drive char(1),freesize bigint)
create table tempdb..#totaldrives_temp(size varchar(100));
insert into  #totaldrives_temp exec xp_cmdshell 'wmic LogicalDisk get deviceid,size'
insert into  #fixeddrives_temp exec xp_fixeddrives

select    f.drive,
        t.[totalsizeMB] as [总空间MB],
        (t.[totalsizeMB]-cast(f.freesize as decimal(20,0))) as [已用空间MB],
        f.freesize as [未用空间MB],
cast(((t.[totalsizeMB]-cast(f.freesize as bigint))*100.0/t.[totalsizeMB]) as decimal(5,1)) as [已使用%],
cast((cast(f.freesize as bigint)*100.0/t.[totalsizeMB]) as decimal(5,1)) as [未使用%]
from 
(

select rtrim(left(LTRIM(size),1)) as drive,
cast(replace(replace(replace(ltrim(RIGHT(rtrim(size),LEN(size)-2)),char(9),''),char(10),''),CHAR(13),'') as bigint)/1024/1024 as [totalsizeMB]

from  #totaldrives_temp 
where size not like '%DeviceID  Size%' and LEN(size) not IN (0,1)
) AS t
inner join #fixeddrives_temp f
on t.drive=f.drive


 

原文地址:https://www.cnblogs.com/fly_zj/p/2485876.html