查看磁盘空间的方法有很多,比如 Powershell, WMIC 等,下面是用T-SQL的方法查看。对于多服务器,可以写游标,也可以在 Register Server 中建立一个组,比如 Production Group,QA Group 等等, 添加相应的服务器到不同的组中,最后在组的节点上应用下面的脚本即可。
create table #a (id int IDENTITY(1,1),DiskName varchar(50))
insert into #a(DiskName)
exec xp_cmdshell 'wmic LOGICALDISK get name'
create table #b (id int IDENTITY(1,1),freespace varchar(50))
insert into #b(freespace)
exec xp_cmdshell 'wmic LOGICALDISK get freespace'
create table #c (id int IDENTITY(1,1),size varchar(50))
insert into #c(size)
exec xp_cmdshell 'wmic LOGICALDISK get size'
select server_name=@@servername,DiskName
,convert(bigint,replace(size,char(13),''))/1024/1024/1024 as total_disk_size_gb
,convert(bigint,replace(#b.freespace,char(13),''))/1024/1024/1024 as free_disk_size_gb
,convert(varchar,convert(decimal(4, 2),(convert(decimal(15, 2),convert(decimal(15, 2),replace(#b.freespace,char(13),''))/1024/1024/1024*100)/
convert(decimal(15, 2),convert(decimal(15, 2),replace(size,char(13),''))/1024/1024/1024))))+'%' as free_space_percent
from #a join #b on #a.id=#b.id join #c on #a.id=#c.id
where #a.id >1 and #b.freespace is not null and charindex(char(13),replace(#b.freespace,' ','')) <>1
drop table #a,#b,#c