私がまとめたもの。これを実行するには、SQL ServerでPOWERSHELLが必要です。バージョンがわからない。これにより、ドライブのサイズが取得され、ファイルサイズと空き領域、および空きドライブ領域が報告されます。
100%オリジナルではなく、その一部をインターネット上の他の場所で見つけて、全体をまとめました。あなたが括弧で遊ぶ必要があるかもしれないので、適切なマージンに収まるようにするのはPITAでした
--drop table ##DB_FILE_INFO
--alter procedure super_drive_info3
--as
if exists ( select * from tempdb.dbo.sysobjects o
where o.xtype in ('U') and o.name in ('##DB_FILE_INFO' ))
drop table ##DB_FILE_INFO
if exists ( select * from tempdb.dbo.sysobjects o
where o.xtype in ('U') and o.name in ('##output'))
drop table ##output
create table ##DB_FILE_INFO (
[server_Name] varchar(255) not null,
[database_name] varchar(255) not null,
[File_ID] int not null,
[File_Type] int not null ,
[Drive] varchar(255) not null ,
[Logical_Name] varchar(255) not null ,
[Physical_Name] varchar(255) not null ,
[FILE_SIZE_MB] int not null ,
[SPace_USED_MB] int not null ,
[Free_space] int not null ,
[Max_SIZE] int not null ,
[Percent_Log_growth_enabled] int not null ,
[growth_rate] int not null ,
[current_date] datetime not null
)
--go
declare @sql nvarchar(4000)
set @sql =
'use ['+'?'+']
--if db_name() <> N''?'' goto Error_Exit
insert into ##DB_FILE_INFO
(
[server_Name],
[database_name],
[File_ID],
[File_Type],
[Drive],
[Logical_Name],
[Physical_Name],
[FILE_SIZE_MB],
[SPace_USED_MB],
[Free_space],
[Max_SIZE],
[Percent_Log_growth_enabled],
[growth_rate],
[current_date]
)
SELECT
@@servername as [Server_Name],
db_name() as database_name,
[file_id] AS [File_ID],
[type] AS [File_Type],
substring([physical_name],1,1) AS [Drive],
[name] AS [Logical_Name],
[physical_name] AS [Physical_Name],
CAST([size] as DECIMAL(38,0))/128. AS [File Size MB],
CAST(FILEPROPERTY([name],''SpaceUsed'') AS DECIMAL(38,0))/128. AS
[Space_Used_MB],
(CAST([size] AS DECIMAL(38,0))/128) -
(CAST(FILEPROPERTY([name],''SpaceUsed'') AS DECIMAL(38,0))/128.) AS [Free
Space],
[max_size] AS [Max_Size],
[is_percent_growth] AS [Percent_Growth_Enabled],
[growth] AS [Growth Rate],
getdate() AS [Current_Date]
FROM sys.database_files'
exec sp_msforeachdb @sql
declare @svrName varchar(255)
declare @sql2 varchar(400)
--by default it will take the current server name, we can the set the server
name as well
set @svrName = @@SERVERNAME
set @sql2 = 'powershell.exe -c "Get-WmiObject -ComputerName ' +
QUOTENAME(@svrName,'''') + ' -Class Win32_Volume -Filter ''DriveType = 3'' |
select name,capacity,freespace |
foreach{$_.name+''|
''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'
--creating a temporary table
CREATE TABLE ##output
(line varchar(255))
--inserting in to temporary table
insert ##output
EXEC xp_cmdshell @sql2;
with Output2
--(drivename, capacity(gb),freespace(gb), always_on_status)
as
(
select @@servername as server_name,
rtrim(ltrim(SUBSTRING(line,1,CHARINDEX(':',line) -1))) as drivename
,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
(CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as float)/1024,2) as
'capacityGB'
,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
(CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as float) /1024 ,2)as
'freespaceGB'
,CASE WHEN (SERVERPROPERTY ('IsHadrEnabled')=1) THEN 'YES' WHEN
(SERVERPROPERTY ('IsHadrEnabled')=0) THEN 'NO'ELSE 'NOT AVAILABLE' END AS
ALWAYS_ON_STATUS
--into #output2
from ##output
where line like '[A-Z][:]%'
--order by drivename
),
DB_FILE_INFO2 as
(
select server_Name,
database_name,
File_ID,
File_Type,
Drive,
Logical_Name,
Physical_Name,
FILE_SIZE_MB,
SPace_USED_MB,
Free_space,
Max_SIZE,
Percent_Log_growth_enabled,
growth_rate
--current_date
from ##DB_FILE_INFO
--inner join #output b on a.drive = b.drivename and a.server_Name =
b.server_name
)
select
getdate() as Today_Date,
a.server_Name,
a.database_name,
a.Drive,
a.Logical_Name,
a.Physical_Name,
a.FILE_SIZE_MB,
a.Space_Used_MB,
--sum(a.SPace_USED_MB) as hg,
a.Free_space as Free_Space_in_File,
--Percentage_file_free = (a.Space_Used_MB/a.FILE_SIZE_MB),
b.capacitygb as Total_Drive_capacity,
b.freespacegb as Total_Free_Space,
c.total_log_size_mb,
c.active_log_size_mb
--,Percentage_free_space = ((cast(Free_space as decimal))/(cast(FILE_SIZE_MB
as decimal)) * 100)
from DB_FILE_INFO2 a
inner join output2 b on a.server_Name = b.server_name and a.Drive =
b.drivename
cross apply sys.dm_db_log_stats (db_id(a.database_name)) c
order by a.Drive, a.database_name
--drop table ##DB_FILE_INFO
--drop table #output